Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 5.5 Classname Resolution

Tags:

php

class

php-5.5

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?

like image 900
Bruno Augusto Avatar asked Jun 27 '14 23:06

Bruno Augusto


1 Answers

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.

TL;DR

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.

Director's Cut

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

like image 91
Bruno Augusto Avatar answered Sep 29 '22 09:09

Bruno Augusto