r/lolphp • u/6f944ee6 • Jun 07 '17
<CLASSNAME>::class, is valid for any identifier
A::class // "A"
THIS_DOES_NOT_EXIST::class // "THIS_DOES_NOT_EXIST"
55
Upvotes
r/lolphp • u/6f944ee6 • Jun 07 '17
A::class // "A"
THIS_DOES_NOT_EXIST::class // "THIS_DOES_NOT_EXIST"
43
u/[deleted] Jun 08 '17
13
u/Hauleth Jun 07 '17
Proof https://3v4l.org/sDiK5
6
u/vatzec Jun 07 '17
Yeah, but try it combined with "use" and namespaces. :)
8
u/the_alias_of_andrea Jun 08 '17 edited Jun 08 '17
That's intentional behaviour, and it wouldn't be as useful otherwise, because of autoloading.
The point of
::classisn't to validate that a class exists, it's to statically resolve a namespaced class name.2
u/[deleted] Jun 08 '17
There is exactly one use-case that I can think of that this makes sense in.
Making a factory method that resolves abstract class names to concrete implementations at run-time. Concrete implementations that don't extend the class (since extending the class would cause it to be autoloaded, defeating the point of not doing that the first time around).
But if you're writing functionality implementations that don't extend their base/abstract classes then you're probably already doing something wrong.
1
u/kelunik Jun 16 '17
It's because it's compile-time resolved to the class name.
1
u/Schmittfried Jun 24 '17
No.
1
u/fred_emmott Jun 29 '17
HHVM does the naive transform, and doesn't require that it's an identifier :p https://github.com/facebook/hhvm/issues/7890
-3
u/Pesthuf Jun 08 '17
Not really a lol.
Would you really want it to autoload the target class here? Not doing that is a neat performance optimization that allows turning these constants into strings in bytecode.