On 12/02/2020 03:58, Mike Schinkel wrote:
Returning a_closure_  instead of a string would be providing a feature 
we_already_  have instead of one we do_not_  have.


Not really, because you still need a way to get the fully-qualified name of the function. This is not valid:

namespace MyVendor\Something\Foo;
function bar() {}

\Closure::fromCallable('bar'); # Error: tries to resolve function '\bar'


So if we had ::function which returned the name, and ::callable that returned a closure, the following would be equivalent:

namespace MyVendor\Something\Foo;
function bar() {}

\Closure::fromCallable('MyVendor\Something\Foo\bar');
\Closure::fromCallable(bar::function);
bar::callable;


One combination would be ::function returning a string, and $() accepting one, which would give:

$(bar::function)


In practice, that would almost certainly be optimised into one instruction, so the main advantage would be consistency, unless there are use cases where a string rather than "any callable" is actually required.


Or today just use fn() => myfunc().


That's not quite the same either: at the very least, you need to define it as fn(...$args) => myfunc(...$args) to pass through the parameters. But Closure::callable is able to copy the signature of the original function, including type information [1], so our hypothetical ::callable could do the same.


[1] https://3v4l.org/XBOmJ

Regards,

--
Rowan Tommins (né Collins)
[IMSoP]

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

Reply via email to