How to call the __invoke method of a member variable inside a class

FYI in PHP 7+ parenthesis around a callback inside an object works now:

class foo {                                                                     
    public function __construct() {                                             
        $this -> bar = function() {                                             
            echo "bar!" . PHP_EOL;                                              

    public function __invoke() {                                                
        echo "invoke!" . PHP_EOL;                                               

(new foo)();                                                                    

$f = new foo;                                                                   
($f -> bar)(); 



There's three ways:

Directly calling __invoke, which you already mentioned:


By assigning to a variable:

$a = $this->a;

By using call_user_func:


The last one is probably what you are looking for. It has the benefit that it works with any callable.

I know this is a late answer, but use a combination of __call() in the parent and __invoke() in the subclass:

class A {
  function __invoke ($msg) {
    print $msg;

class B {
    private $a;

    public function __construct() { $this->a = new A(); }

    function __call($name, $args)
      if (property_exists($this, $name))
        $prop = $this->$name;
        if (is_callable($prop))
          return call_user_func_array($prop, $args);

Then you should be able to achieve the syntactic sugar you are looking for:

$b = new B();
$b->a("Hello World\n");