Re: [RFC] [Discussion] nameof
| From: | Larry Garfield | Date: | Tue, 16 May 2023 21:56:16 +0000 |
| Subject: | Re: [RFC] [Discussion] nameof | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-120321@lists.php.net to get a copy of this message | ||
On Sat, May 13, 2023, at 7:27 AM, Robert Landers wrote:
> Hello Internals,
>
> It is with much trepidation and excitement that I'd like to announce
> the
nameof RFC (https://wiki.php.net/rfc/nameof). It has
> changed
> quite a bit in the last couple of days, so if you haven't seen the
> latest draft, please check it out.
>
> Essentially, it allows using nameof() anywhere a string can be
> used,
> even in static contexts. From a developer's perspective, it is a
> string and from the engine's perspective, it is also mostly a string
> (depending on how deep we want to go on error checking -- see the
> RFC).
>
> If anything is unclear or if I missed something, please let me know.
>
> Robert Landers
> Software Engineer
> Utrecht NL
Some concrete use cases that I know I run into, and would thus be what I'd hope an RFC like
this would resolve:
Router::addRoute('\my\space\my_action_function`);
Right now that has to be a string with a full namespace. You cannot use a FCC here, because you
want to use this data to compile the router somehow. So in this case we want the full namespace.
(Ignore function autoloading for now.)
Router::addRoute(MyClass::actionMethod);
Various frameworks have different custom syntaxes for this case. It's also not clear if this
refers to a static method, or a "instantiate this out of the container first and then call this
method" approach. In these cases, we would want the full class name, and the method name on
its own, as separate strings.
In my FP library, I have code like this:
function prop(string $prop): \Closure
{
return static fn (object $o): mixed => $o->$prop;
}
function method(string $method, ...$args): \Closure
{
return static fn (object $o): mixed => $o->$method(...$args);
}
Which you can then use in a pipe, like so:
pipe($someObject, method('foo'), prop('bar'));
Or, more realistically, you'd use method() and prop() in a map or filter call within the pipe.
In this case, you want just the method/property name on its own, without a namespace, because it
will be used in the context of an object to be named later.
How would nameof() handle each of these cases?
--Larry Garfield