You cannot call the function this way. That's because PHP allows to have variables an functions having the same name in a class. If you use the function operator ()
PHP will only look into the list of functions and don't look at variables that are closures.
As of PHP5.4 a solution could look like this:
class MyClass {
public function __call($fname, $args) {
// bind the `this` scope to use
$cl = $this->{$fname}->bindTo($this);
// call the function and pass args to it
return call_user_func_array($cl, $args);
}
}
Example:
$obj = new MyClass();
$obj->func = function() {
echo 'We are in ' . get_class($this);
};
$obj->func(); // We are in MyClass
You can test this here