122

I would like to be able to call a closure that I assign to an object's property directly without reassigning the closure to a variable and then calling it. Is this possible?

The code below doesn't work and causes Fatal error: Call to undefined method stdClass::callback().

$obj = new stdClass();
$obj->callback = function() {
    print "HelloWorld!";
};
$obj->callback();
1

12 Answers 12

130

As of PHP7, you can do

$obj = new StdClass;
$obj->fn = function($arg) { return "Hello $arg"; };
echo ($obj->fn)('World');

or use Closure::call(), though that doesn't work on a StdClass.


Before PHP7, you'd have to implement the magic __call method to intercept the call and invoke the callback (which is not possible for StdClass of course, because you cannot add the __call method)

class Foo
{
    public function __call($method, $args)
    {
        if(is_callable(array($this, $method))) {
            return call_user_func_array($this->$method, $args);
        }
        // else throw exception
    }
}

$foo = new Foo;
$foo->cb = function($who) { return "Hello $who"; };
echo $foo->cb('World');

Note that you cannot do

return call_user_func_array(array($this, $method), $args);

in the __call body, because this would trigger __call in an infinite loop.

1
  • 3
    Sometimes you will find this syntax usefull - call_user_func_array($this->$property, $args); when it is about callable class property, not a method. Commented Jun 18, 2014 at 9:19
112

You can do this by calling __invoke on the closure, since that's the magic method that objects use to behave like functions:

$obj = new stdClass();
$obj->callback = function() {
    print "HelloWorld!";
};
$obj->callback->__invoke();

Of course that won't work if the callback is an array or a string (which can also be valid callbacks in PHP) - just for closures and other objects with __invoke behavior.

5
  • 3
    @marcioAlmada very ugly though.
    – Mahn
    Commented Jul 6, 2014 at 14:19
  • 1
    @Mahn I think it's more explicit than the accepted answer. Explicit is better in this case. If you really care for a "cute" solution, call_user_func($obj->callback) is not that bad.
    – marcio
    Commented Jul 6, 2014 at 16:07
  • But call_user_func works with strings too and that's not always convinient
    – Gherman
    Commented Dec 12, 2014 at 8:29
  • 2
    @cerebriform semantically it makes no sense to have to do $obj->callback->__invoke(); when one would expect it to be $obj->callback(). It's just a question of consistency.
    – Mahn
    Commented Feb 13, 2015 at 22:50
  • 3
    @Mahn: Granted, it's not consistent. Consistency has never really been PHP strong suit, though. :) But I think it does makes sense, when one considers the object nature: $obj->callback instanceof \Closure.
    – bishop
    Commented Feb 14, 2015 at 2:17
29

As of PHP 7 you can do the following:

($obj->callback)();
1
  • Such common sense, yet it's purely badass. The great power of PHP7!
    – tfont
    Commented Sep 6, 2018 at 12:41
11

Since PHP 7 a closure can be called using the call() method:

$obj->callback->call($obj);

Since PHP 7 is possible to execute operations on arbitrary (...) expressions too (as explained by Korikulum):

($obj->callback)();

Other common PHP 5 approaches are:

  • using the magic method __invoke() (as explained by Brilliand)

    $obj->callback->__invoke();
    
  • using the call_user_func() function

    call_user_func($obj->callback);
    
  • using an intermediate variable in an expression

    ($_ = $obj->callback) && $_();
    

Each way has its own pros and cons, but the most radical and definitive solution still remains the one presented by Gordon.

class stdKlass
{
    public function __call($method, $arguments)
    {
        // is_callable([$this, $method])
        //   returns always true when __call() is defined.

        // is_callable($this->$method)
        //   triggers a "PHP Notice: Undefined property" in case of missing property.

        if (isset($this->$method) && is_callable($this->$method)) {
            return call_user_func($this->$method, ...$arguments);
        }

        // throw exception
    }
}

$obj = new stdKlass();
$obj->callback = function() { print "HelloWorld!"; };
$obj->callback();
7

It seems to be possible using call_user_func().

call_user_func($obj->callback);

not elegant, though.... What @Gordon says is probably the only way to go.

0
7

Well, if you really insist. Another workaround would be:

$obj = new ArrayObject(array(),2);

$obj->callback = function() {
    print "HelloWorld!";
};

$obj['callback']();

But that's not the nicest syntax.

However, the PHP parser always treats T_OBJECT_OPERATOR, IDENTIFIER, ( as method call. There seems to be no workaround for making -> bypass the method table and access the attributes instead.

7

I know this is old, but I think Traits nicely handle this problem if you are using PHP 5.4+

First, create a trait that makes properties callable:

trait CallableProperty {
    public function __call($method, $args) {
        if (property_exists($this, $method) && is_callable($this->$method)) {
            return call_user_func_array($this->$method, $args);
        }
    }
}

Then, you can use that trait in your classes:

class CallableStdClass extends stdClass {
    use CallableProperty;
}

Now, you can define properties via anonymous functions and call them directly:

$foo = new CallableStdClass();
$foo->add = function ($a, $b) { return $a + $b; };
$foo->add(2, 2); // 4
2
  • Wow :) This is much more elegant than what I was trying to do. My only question is the same as for british hot/cold tap connector elements: WHY is it not built in already??
    – dkellner
    Commented Nov 18, 2015 at 20:40
  • Thanks :). It's probably not built in because of the ambiguity it creates. Imagine in my example, if there was actually a function called "add" and a property called "add". The presence of the parenthesis tells PHP to look for a function with that name.
    – SteveK
    Commented Dec 3, 2015 at 20:50
2

well, it should be emphisized that storing the closure in a variable, and call the varible is actually (wierdly) faster, depending on the call amount, it becomes quite a lot, with xdebug (so very precise measuring), we are talking about 1,5 (the factor, by using a varible, instead of directly calling the __invoke. so instead , just store the closure in a varible and call it.

1

Here's another alternative based on the accepted answer but extending stdClass directly:

class stdClassExt extends stdClass {
    public function __call($method, $args)
    {
        if (isset($this->$method)) {
            $func = $this->$method;
            return call_user_func_array($func, $args);
        }
    }
}

Usage example:

$foo = new stdClassExt;
$foo->blub = 42;
$foo->whooho = function () { return 1; };
echo $foo->whooho();

You are probably better off using call_user_func or __invoke though.

1

Updated:

$obj = new stdClass();
$obj->callback = function() {
     print "HelloWorld!";
};

PHP >= 7 :

($obj->callback)();

PHP >= 5.4 :

$callback = $obj->callback;  
$callback();
1
  • Did you try this? It doesn't work. Call to undefined method AnyObject::callback() (Class AnyObject exists, of course.) Commented Jun 15, 2015 at 19:52
0

If you're using PHP 5.4 or above you could bind a callable to the scope of your object to invoke custom behavior. So for example if you were to have the following set up..

function run_method($object, Closure $method)
{
    $prop = uniqid();
    $object->$prop = \Closure::bind($method, $object, $object);
    $object->$prop->__invoke();
    unset($object->$prop);
}

And you were operating on a class like so..

class Foo
{
    private $value;
    public function getValue()
    {
        return $this->value;
    }
}

You could run your own logic as if you were operating from within the scope of your object

$foo = new Foo();
run_method($foo, function(){
    $this->value = 'something else';
});

echo $foo->getValue(); // prints "something else"
0

I note that this works in PHP5.5

$a = array();
$a['callback'] = function() {
    print "HelloWorld!";
};
$a['callback']();

Allows one to create a psuedo-object collection of closures.

Not the answer you're looking for? Browse other questions tagged or ask your own question.