Re: Introduce the abiltiy to use the first-call-callable syntax on non-static methods, statically
| From: | Robert Landers | Date: | Tue, 24 Jan 2023 11:36:27 +0000 |
| Subject: | Re: Introduce the abiltiy to use the first-call-callable syntax on non-static methods, statically | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119415@lists.php.net to get a copy of this message | ||
On Tue, Jan 24, 2023 at 12:06 PM Ollie Read <php@ollie.codes> wrote:
>
> You're absolutely correct, the problem I was trying to solve was the ability to reference
> a method without throwing around magic strings, apologies if that wasn't clear.
>
> The whole idea, or rather, need/want for it came about because of time spent with Javas
> functional side, though I understand that's safer to do because of the way Java handles type
> safety.
>
> The ::class pseudo type is great for providing a classes FQN, and class-string is great for
> ensuring values are valid class strings when using static analysis, but we don't have anything
> for methods, outside of IDE completion.
>
> The solution wouldn't even need to generate a closure, TBH, it would generate something
> that could then be used in the same manner.
>
> Using the current features of PHP, I'd probably imagine something that does just about the
> same as this:
>
> $method = new ReflectionMethod(MyClass::class, 'method');
>
> $collection->filter($method);
>
> Where the filter method does:
>
> foreach ($items as $item) {
> if ($method->invoke($item)) {
> //
> }
> }
>
> Although reflection doesn't have the overhead that everyone thinks it does, this is a less
> than ideal solution, but I think it accurately portrays what I'm looking for.
>
> ---
> Best Regards,
> *Ollie Read*
> The ::class pseudo type is great for providing a classes FQN, and class-string is great for
> ensuring values are valid class strings when using static analysis, but we don't have anything
> for methods, outside of IDE completion.
>
> The solution wouldn't even need to generate a closure, TBH, it would generate something
> that could then be used in the same manner.
In C#, they have the
nameof() operator that returns the name of the
symbol passed. So you still end up with a string, but the symbol can
easily be refactored.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof
Maybe something like this could help address the problem?
Robert Landers
Software Engineer
Utrecht NL