Andrea + Marco

Is the advantage of `Closure#call()` only performance? Because I don't see
other advantages otherwise, and I don't think the performance impact is
relevant.

I like the proposal, but with some modifications. I am unsure if there is another RFC out there on function de-referencing, but at current the following is not possible:

  $x = function() { return function () { echo 'hi'; }; };
  $x()();

Nor is de-referencing of a closure assigned to an object property:

  class Foo { public $bar; }
  $f = new Foo;
  $f->bar = function () { echo 'hi'; };
  $f->bar();

While those are less important on their own, the use case that this does address is the ability to inline call a closure assigned to an object(/or class) property and fluently without call_user_func:

  class Foo { public $bar; }
  $f = new Foo;
  $f->bar = function () {
    echo 'hi'; return function () {
      echo ' world';
    };
  };
  call_user_func(call_user_func($f->bar));

"->call()" would facilitate the following syntax, which I'd argue has greater semantic meaning than the call_user_func() variant:

  $f->bar->call()->call();

Additionally, the above assumes the following modification to the RFC: the additional of bindCall(), or something similarly named. The purpose here is to be able to call(/* args */) an existing closure with the current bound context (as a result from a previous bindTo) and bindCall($context, /* args */); to support the bind-and-call workflow.

The reason for call() to operate on existing bound context would be this workflow:

  class Foo {
    public $bar;
    public function registerBar(\Closure $c) {
      // bind to specific scope as per Foo's specifications
      $this->bar = $c->bindTo(...);
    }
  }
  $f = new Foo;
  $f->registerBar(function () {
    echo 'hi'; return function () {
      echo ' world';
    };
  });

  $f->bar->call();

  // if you must change context:

  $f->bar->bindCall($otherContext);


Thanks,
Ralph Schindler




--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to