Note that there are some explanatory texts on larger screens.

plurals
  1. POIs is possible to store a reference to an object method?
    primarykey
    data
    text
    <p>Assume this class code:</p> <pre><code>class Foo { function method() { echo 'works'; } } </code></pre> <p>Is there any way to store a reference to the <code>method</code> method of a <code>Foo</code> instance?</p> <p>I'm just experimenting and fiddling around, my goal is checking whether PHP allows to call <code>$FooInstance-&gt;method()</code> without writing <code>$FooInstance-&gt;</code> every time. I know I could write a function wrapper for this, but I'm more interested in getting a reference to the instance method.</p> <p>For example, this pseudo-code would theoretically store <code>$foo-&gt;method</code> in the <code>$method</code> variable:</p> <pre><code>$foo = new Foo(); $method = $foo-&gt;method; //Undefined property: Foo::$method $method(); </code></pre> <p>Apparently, as <code>method</code> is a method and I'm not calling it with <code>()</code> the interpreter thinks I'm looking for a property thus this doesn't work.</p> <p>I've read through <a href="http://www.php.net/manual/en/language.references.return.php" rel="nofollow">Returning References</a> but the examples only show how to return references to variables, not methods.</p> <p>Therefore, I've adapted my code to store an anonymous function in a variable and return it:</p> <pre><code>class Foo { function &amp;method() { $fn = function() { echo 'works'; }; return $fn; } } $foo = new Foo(); $method = &amp;$foo-&gt;method(); $method(); </code></pre> <p>This works, but is rather ugly. Also, there's no neat way to call it a single time, as this seems to require storing the returned function in a variable prior to calling it: <code>$foo-&gt;method()();</code> and <code>($foo-&gt;method())();</code> are syntax errors.</p> <p>Also, I've tried returning the anonymous function directly without storing it in a variable, but then I get the following notice:</p> <blockquote> <p><strong>Notice</strong>: Only variable references should be returned by reference</p> </blockquote> <p>Does this mean that returning/storing a reference to a class instance method is impossible/discouraged or am I overlooking something?</p> <hr> <p><strong>Update:</strong> I don't mind adding a getter if necessary, the goal is just getting a reference to the method. I've even tried:</p> <pre><code>class Foo { var $fn = function() { echo 'works'; }; function &amp;method() { return $this-&gt;fn; } } </code></pre> <p>But from the <code>unexpected 'function' (T_FUNCTION)</code> error I'd believe that PHP wisely doesn't allow properties to store functions.</p> <p>I'm starting to believe that my goal isn't easily achievable without the use of ugly hacks as <code>eval()</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload