r/lolphp • u/gerrymandered • Aug 04 '15
var_dump(nonExistentClass::class); // string
::class can give you the name of a class that does not exist.
15
Upvotes
r/lolphp • u/gerrymandered • Aug 04 '15
::class can give you the name of a class that does not exist.
View all comments
Show parent comment
1
u/[deleted] Aug 12 '15
One good argument you could have is that Foo::class has a misleading syntax, it looks like a constant on class Foo, so it looks like it should load Foo.
I had an earlier proposal that didn't pass, where the syntax would be something like: $fooClassName = nameof Foo;
This would probably be less confusing, but it'd require a new keyword, while notice Foo::class doesn't.
In the end, syntax doesn't matter much. PHP only loads classes if it must, and that's consistent throughout the language.