What happens to class name resolution in php 5.5?

Asked

Viewed 366 times

4

PHP 5.5 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 class doesn’t exist, we get out the same way.

Example:

echo ClasseNaoDeclarada::class; //ClasseNaoDeclarada

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

  • 6

    Why exactly I believe that only those involved in the development of PHP 5.5 (or that understand C) would be able to answer but, if I had to guess, I would say that it is because of/Late Static Binding that would delay the resolution until the operator :: (T_PAAMAYIM_NEKUDOTAYIM) assuming the class associated with it.

  • 2

    I couldn’t figure out why, but anyone who wants to research can look at proposal for such an appeal and the pull request in the repository.

  • 1

    So, from what I understand, the idea would be to just get 'Fully Qualified' class names effortlessly and more elegantly using aliases rather than gigantic strings with possible errors. @bfavaretto good tip, by the way

  • I’ve been looking for this and I don’t think kkkkkkkkkk one time we find !!!! kkkkkk

  • 2

    Let’s see if now stimulates the topic ^_^

  • In no way can an undeclared class be evaluated even if it is an external attribute. I hope I have helped

  • @Nathan130200: Helping, helping, not helping (hehe). The whole discussion is exactly why this alien works when it should throw an error precisely because the class has not been declared.

  • This statement also does not generate an error when the class does not exist. Could there be something correlated with it? function foo(TTT $a){

}

  • @Wallacemaxters probably yes. See the answer extended below

  • What was the reason for -1? Could you show how I can improve the question?

Show 5 more comments

2 answers

4


Finally an official response... in terms. It was given to me by someone identified by [email protected] from of this bug that I myself reported. It’s just the caveat that I don’t know exactly how involved with language development that person is.

TL;DR

PHP does not need the definition of a class to know its full name. Everything he needs he gets at compile time and therefore does not need to load it.

Director’s version

Namespaces as well as the you use are solved at compile time, that is, when PHP compiles the file before of its execution. For this reason there are specific requirements on how they can be used.

Because of all these requirements, when PHP finds the name of a class it already knows, in readiness, its full name. Imagining this as a file system, the namespace would be like a directory for the relative locations and the use would be symbolic links (symlinks).

The class name can be either absolute ("Testing Test") or relative ("Test") and if relative can be a normal name[lacks context].

namespace Testing {
    echo Test::class; // \Testing + Test = \Testing\Test
}

Or a alias:

use Testing\Test as AliasedTest;
echo AliasedTest::class; // AliasedTest + use = \Testing\Test

Without all this the self-loading class would not function!

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

  • Ball show! thank you so much for the reply!

  • All credit goes to him or her who took the trouble to provide that brief explanation through an unsuitable vehicle for both. It was really surprising the good mood of him(a) in view of the staff of bugs always come with your shoes on us.

3

So the idea would be to just get 'Fully Qualified' class names effortlessly and more elegantly using aliases rather than 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 pull request you can see the case: ZEND_FETCH_CLASS_DEFAULT that builds the name of the class regardless of its 'existence' or not. In test cases the first test is precisely of a class that does not exist and was not created in Runtime.

  • It makes sense, but only as a consideration, these, as far as I am concerned, are constant in the pull request. So far the removed fragment prevails, constant to lines 2166-2172. What exactly on this fragment indicates the motive of an undeclared class returning something instead of issuing a Fatal Error? Another thing... this one switch is based on a function that I imagine to be part of the engine. But if there is a switch, there are conditions to comply with then, such behavior would be customizable?

  • 1

    Commits (3, including the resolution of a memory Leak) were apparently accepted yes by that message and the code I saw from the PHP-5.5.14 branch is there and the switch has even been improved.

  • In time, the PHP-5.5 branch already has, including

Browser other questions tagged

You are not signed in. Login or sign up in order to post.