Little brother has a puzzled, hope to mention point! Appreciate it!
PHP instantiates a class, the class name of the secondary class is the same as one of the method names, and the method is run directly after instantiation, which is why?
abc();?>
But after the addition of namespaces, this phenomenon is not, what is the reason?
As shown in the code above, after the namespace is annotated, it outputs 111 directly after instantiation. But with namespaces you need to call them manually!
Reply content:
Little brother has a puzzled, hope to mention point! Appreciate it!
PHP instantiates a class, the class name of the secondary class is the same as one of the method names, and the method is run directly after instantiation, which is why?
abc();?>
But after the addition of namespaces, this phenomenon is not, what is the reason?
As shown in the code above, after the namespace is annotated, it outputs 111 directly after instantiation. But with namespaces you need to call them manually!
A method with the same name as a class is equivalent to a constructor, that is function __construct(){}
,
For backwards compatibility, if PHP 5 cannot find the __construct () function in the class and does not inherit one from the parent class, it will attempt to find the old constructor, which is the function with the same name as the class. So the only thing that can create a compatibility problem is when a method named __construct () already in the class is used for other purposes.
It is not recommended to use the same function name as the class name
A second question
From PHP 5.3.3, in a namespace, a method with the same name as the class name is no longer a constructor. This change does not affect classes that are not in the namespace.
So after the namespace has been annotated, the instantiation is followed by a direct output of 111. But with namespaces you need to call them manually!
Documents are spoken, look closely: http://php.net/manual/zh/language.oop5.decon.php
Your question is explained in detail in the constructor of PHP, here I quote:
First, in PHP5, the standard constructor is __construct, but in the previous PHP version, its constructor is a function with the same name as the class, in order to be compatible with the old PHP version, this feature is preserved, and explains your first question: When the class name and one of the method names are the same, Instantiation class executes the same name method
Secondly:
From PHP 5.3.3, in a namespace, a method with the same name as the class name is no longer a constructor. This change does not affect classes that are not in the namespace.
Officially stated.
PS: It is best to take a look at the official documentation when you have a problem, and many times you can solve this problem by yourself through this official document
Simple is a compatibility problem, the old version of the PHP constructor is the same as the class name, the old version should be PHP4 bar, 5 introduced the construct function as a constructor, in order to be compatible, when the class can not find the construct function, and also does not inherit the parent class constructor, A function that has the same name as the class is executed as a constructor, but the rule is invalidated in the namespace, guessing that the namespace is a new thing, so don't worry about compatibility. Phone typing is not good to underline, construct front has two underline