I'm trying to run the $greeter function property of the $greeter instance of the Greeter class. I've read the answers from this related post but could not get them to work (the post also mentions _call, traits, stdClass, returning a function from a function (which doesn't make sense to me why this works without having to call twice), and the given solutions seem like overkill for the simple thing I'm trying to achieve). Perhaps my case is a little different. I don't understand why the parser messes up.
class Greeter {
private $greeter;
function __construct() {
$this->greeter = function() {
echo "Hello!\n";
};
}
public function greet() {
$this->greeter();
}
}
// THIS WORKS AS EXPECTED:
$hello = function() { echo "Hi!\n"; };
$hello();
$greeter = new Greeter();
// NEITHER OF THESE WORK:
call_user_func($greeter->greet);
$greeter->greet();
$greeter['greet']();
OUTPUT:
Hi!
<br />
<b>Warning</b>: call_user_func() expects parameter 1 to be a valid callback, no array or string given on line <b>30</b><br />
<br />
<b>Fatal error</b>: Call to undefined method Greeter::greeter() on line <b>15</b><br />