Re: [RFC] Closure::call and Function Referencing as Closures

From: Date: Mon, 04 Aug 2014 19:29:16 +0000
Subject: Re: [RFC] Closure::call and Function Referencing as Closures
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-76345@lists.php.net to get a copy of this message
Andrea Faulds wrote (on 04/08/2014):
On 4 Aug 2014, at 01:53, Thomas Bley <mails@thomasbley.de> wrote:
from userland perspective, I would prefer to open the Closure constructor instead of adding new syntax: $qux = new FooBar(3); // $func = &FooBar::getStatic; $func = new Closure(array('FooBar', 'getStatic')); $func($qux); // 3
You can actually do that already in a convoluted way:
     $func = (new ReflectionMethod(“FooBar::getStatic”)->getClosure());
Though it must be explicitly bound in the case of a non-static method.
The advantage of a constructor / factory method approach is that it allows you to promote existing "callable" arguments to Closures. If passed a Closure, it could return it unchanged. Thus: function rebind_callable(callable $c, $newthis) { return (new Closure($c))->bindTo($newthis); } $foo_rebound = rebind_callable( [$foo, 'doSomething'], $bar ); I guess the disadvantage is it requires an intermediate value (string/array) rather than being something as low-level as a parser rule. I tend to agree with Stas that overloading the & operator here could be confusing. -- Rowan Collins [IMSoP]

« previous php.internals (#76345) next »