Re: [RFC] Allow ::class on objects
| From: | Rowan Tommins | Date: | Fri, 10 Jan 2020 21:44:30 +0000 |
| Subject: | Re: [RFC] Allow ::class on objects | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108094@lists.php.net to get a copy of this message | ||
On 10/01/2020 17:29, Larry Garfield wrote:
But having some kind of working way to reference a function that doesn't involve concatenating a string onto a namespace constant would be*super* nice. Whether it's called ::func or ::nameof I don't much care. Viz, replace this: $func = __NAMESPACE__ . '\myfunc`'; With this: use My\Code\myfunc; $func = myfunc::nameof;Note that the above example should be: use function My\Code\myfunc; $func = myfunc::nameof; That's important, because we couldn't actually have one operator that resolved both types, either at compile time or run time. For instance, this is valid code: namespace Whatever; use Bar\Baz as foo; use function Something\other as foo; echo foo::class; // "Bar\Baz" foo(); // runs Something\other(), not Bar\Baz() A ::func operator would also need to deal with everybody's favourite name resolution: the dynamic fallback to global functions. In other words, this... namespace Foo; echo strlen::func; ...would need to print "Foo\strlen" if it was defined, and "strlen" if not. That would then make it odd if a completely undefined function didn't error, so I don't think it could match the current behaviour of ::class. Regards, -- Rowan Tommins (né Collins) [IMSoP]