What happens with class name resolution in php 5.5?

4

PHP 5.5 has implemented a new feature, which consists of getting the class name through the keyword class :

Example:

namespace testando;
class Teste{}

echo Teste::class; // testando\Teste;

This works correctly, as expected.

Now, I'd like to understand why, when the class does not exist, we get the output in the same way.

Example:

echo ClasseNaoDeclarada::class; //ClasseNaoDeclarada

Is there any special reason to get this name (where the class has not been declared)?

    
asked by anonymous 24.06.2014 / 19:37

2 answers

4

Finally an official response ... in terms. It was given to me by someone identified by [email protected] from this bug

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 the autoloading of classes would not work!

::class is just a new way to expose this information that PHP already knows.

    
30.06.2014 / 20:47
3

So the idea would be to get fully qualified class names effortlessly and more elegantly using aliases instead of gigantic strings with possible errors.

The name is given according to class type and the namespace and types are ZEND_FETCH_CLASS_SELF , ZEND_FETCH_CLASS_PARENT , ZEND_FETCH_CLASS_STATIC , ZEND_FETCH_CLASS_DEFAULT .

In the pull request you can see the case: ZEND_FETCH_CLASS_DEFAULT that constructs the class name independent of its 'existence' or not. In test cases the first test is just a class that does not exist and was not created in runtime.

    
27.06.2014 / 16:10