PHP 5.5 has implemented as a new feature a new way to retrieve the classname through the syntax ::class
:
<?php
namespace Testing;
class Test{}
echo Test::class; // Testing\Test;
This works perfectly, alright? BUt what me and some other friends wanted to know is why this syntax also returns a classname when used alongside an undeclared class. E.g.:
<?php
echo UndeclaredClass::class; // UndeclaredClass
In several other cases an error is raised, but not here. Anyone know, with concrete basis if possible, why does this happen?
Does it have anything to Late Static Bindings or it's just a (temporary) limitation/bug of this brand new feature?
Finally an official answer... relatively speaking. It was presented to me by someone identified by [email protected] in a bu report i created today. The only exception is about how involved with PHP development this person is.
PHP doesn't need ot know the definition of a class to get its fully-qualified name. All the required informations are available in compile-time so it doesn't need to load it.
Namespaces like the uses are resolved in compile-time, i.e., when the file is compiled before its execution. That's why there are strict requirements in order to use them.
Because of all of those requirements, when PHP encounters a class name it can immediately know its fully-qualified name. Thinking of it as a filesystem the namespace would be a directory for relative locations and the use would be symlinks.
The class name is either absolute ("\Testing\Test") or relative ("Test"), and if relative it could be a normal name. [more context required]
namespace Testing {
echo Test::class; // \Testing + Test = \Testing\Test
}
Or an alias:
use Testing\Test as AliasedTest;
echo AliasedTest::class; // AliasedTest + use = \Testing\Test
Without all of this autoloading wouldn't work!
::class
is just a new tool to expose information PHP has always known.
This "extended answer" is pretty much the same of what I received as bug report. The reason of so much apparent copy & paste is because, originally, I built up this answer for another Stack Overflow Community
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With