PHP has had Closures since 5.3, and since 5.4 has had Closure::bind (static method) and Closure::bindTo (method) to allow creating new closures that have $this bound to a specific method. However, it has not been possible to bind at call-time, and you must instead create a temporary new closure, making calling bound to multiple objects cumbersome and inefficient (at least two statements are needed, and a new closure must be created and immediately disposed of for each).

A new method is added to Closure , with the following signature:

mixed Closure :: call ( object $to [ , mixed ... $parameters ] )

It calls the closure with the given parameters and returns the result, with $this bound to the given object $to , using the scope of the given object. Like the bind ( To ) methods, a static class cannot be bound (using ->call will fail).

It can be used like so:

$foo = new StdClass ; $foo -> bar = 3 ; $foobar = function ( $qux ) { var_dump ( $this -> bar + $qux ) ; } ; $foobar -> call ( $foo , 4 ) ; // prints int(7)

The ->call method, unlike bind ( To ), does not take a scope parameter. Instead, it will always use the class of the object as its scope. Thus:

class Foo { private $x = 3 ; } $foo = new Foo ; $foobar = function ( ) { var_dump ( $this -> x ) ; } ; $foobar -> call ( $foo ) ; // prints int(3)