Andrea Faulds wrote (on 04/08/2014):
On 4 Aug 2014, at 01:53, Thomas Bley <ma...@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]

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

Reply via email to