Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

PHP 5.4.5, here. I'm trying to invoke an object which is stored as a member of some other object. Like this (very roughly)

class A {
    function __invoke () { ... }
}

class B {
    private a = new A();
 ...
    $this->a();  <-- runtime error here
}

This produces a runtime error, of course, because there's no method called a. But if I write the call like this:

($this->a)();

then I get a syntax error.

Of course, I can write

$this->a->__invoke();

but that seems intolerably ugly, and rather undermines the point of functors. I was just wondering if there is a better (or official) way.

like image 770
Jules May Avatar asked Sep 26 '12 13:09

Jules May


2 Answers

There's three ways:

Directly calling __invoke, which you already mentioned:

$this->a->__invoke();

By assigning to a variable:

$a = $this->a;
$a();

By using call_user_func:

call_user_func($this->a);

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

like image 58
igorw Avatar answered Oct 09 '22 12:10

igorw


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)(); 

Result:

invoke!
bar!
like image 25
drmad Avatar answered Oct 09 '22 13:10

drmad