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

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

Reply via email to