PHP RFC: Closure::call

  • Version: 0.3
  • Author: Andrea Faulds, ajf@ajf.me
  • Status: Accepted






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;

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

call would be useful in many cases where bindTo is used (e.g. search of GitHub for bindTo). A search on GitHub reveals many using bindTo and immediately calling with call_user_func, which would now not be necessary as they could just use call.




$a = function () {
    return $this->x;
class FooBar {
    private $x = 3;
$foobar = new FooBar;
$a = function () {
    return $this->x;
class FooBar {
    private $x = 3;
$foobar = new FooBar;

b.php shows a 2.18x improvement over a.php:


real	0m1.877s
user	0m1.835s
sys	0m0.025s


real	0m0.859s
user	0m0.826s
sys	0m0.018s

Backward Incompatible Changes and RFC Impact

This has no effect on backwards compatibility.

Proposed PHP Version(s)


Future Scope




Voting started 2014-08-17 but was cancelled the same day due to the removal of unbound scoped closures.

Voting started again on 2014-08-20 and ended 2014-08-27.

Closure::apply() (Approve RFC and merge into master?)
Real name Yes No
aharvey (aharvey)  
ajf (ajf)  
bishop (bishop)  
daverandom (daverandom)  
dm (dm)  
dragoonis (dragoonis)  
guilhermeblanco (guilhermeblanco)  
irker (irker)  
jedibc (jedibc)  
kalle (kalle)  
kriscraig (kriscraig)  
rdlowrey (rdlowrey)  
weierophinney (weierophinney)  
Final result: 13 0
This poll has been closed.

Patches and Tests


There is a pull request for review purposes here: https://github.com/php/php-src/pull/775



  • v0.3 - Removed unbound scoped closures, made →call use class of object as its scope
  • v0.2.1 - Added performance section
  • v0.1 - Initial version
rfc/closure_apply.txt · Last modified: 2017/09/22 13:28 (external edit)