> On Feb 11, 2020, at 9:51 AM, Chase Peeler <[email protected]> wrote:
>
> Can anyone thing of a use-case where you would want a string name of a
> function and a callable would not be acceptable, besides possibly debugging
> code that said 'echo "I'm calling ".myfunction::function;'? Everything that
> I can think of that accepts a function name, also accepts a callable (e.g.
> array_map), but I could be forgetting something.
>
> If not, then I think it makes sense to return a callable. It might not be
> entirely consistent with the behavior of ::class, but, a class isn't
> entirely consistent with a method/function either, so I think there is some
> latitude for small differences.
>
> As for the ::func vs ::function. I think ::function is safer, since it's a
> reserved word. Otherwise you might run into issues with something like this:
>
> class foo {
> const func = "bar";
> }
>
> function foo(){}
>
> echo foo::func;
>
> Probably not something that happens very often, but, I think the 4 extra
> characters to prevent it would be worth it.-------
Returning a _closure_ instead of a string would be providing a feature we
_already_ have instead of one we do _not_ have.
If we had ::function returning a string we could use Closure::fromCallable() to
get a closure. Or today just use fn() => myfunc().
But if ::function instead returned a closure then there still would be no way
to extract the name of a function as a string from a symbol where PHP can throw
a warning if it does not recognize the symbol, such as in the case of a typo.
Seems to me having a shorter syntax to get a closure is an orthogonal concern.
If we want a shorthand for closure we should create an additional syntax for it
but still provide a way to extract a function's name as a string from its
symbol since that is currently _not_ possible. Getting a closure from a
function symbol currently _is_ possible.
Much better to provide ::function to return the name of the function and
::closure get a closure that can call the function.
Or have ::function to return the name of the function and provide a syntax
something like ${myfunc} to return a closure, which has been suggested later in
this thread.
Or best would be to add ::nameof for functions, method, classes, interfaces and
traits and provide access to closures using either ::closure or a new short
syntax.
-Mike
P.S. A language I specialized in during the late 80's and early 90's called
Clipper implemented closures with the following syntax:
- Without parameters: {|| expr } equivalent to function(){ return expr; } and
fn() => expr;
- With parameters: {|a,b| expr } equivalent to function(a,b){ return expr; }
and fn(a,b) => expr;
If we want a shorter syntax for function closures than fn() => myfunc() maybe
we consider {|| myfunc() } which would be more general purpose than only
returning a closure for a function?
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php