Php questions about variable class names

Source: Internet
Author: User
{Code...} So the question is, I have already introduced the namespace above. why can't the new $ a1 () class be found and newA () is normal ?? A variable class name can be used only when all namespaces are written. The introduced namespace does not take effect. Why? I hope you can help me solve this problem.
Namespace a; class A {public function _ construct () {echo 'A' ;}} namespace B; use a \ a; $ a1 = 'a '; $ a2 = 'a \ a'; // new $ a1 (); // error // new $ a2 (); // normal // new (); // normal

So the question is, I have already introduced the namespace above. Why?new $a1()Class not found, and directnew A()Normal ?? A variable class name can be used only when all namespaces are written. The introduced namespace does not take effect. Why? I hope you can help me solve this problem.

Reply content:
Namespace a; class A {public function _ construct () {echo 'A' ;}} namespace B; use a \ a; $ a1 = 'a '; $ a2 = 'a \ a'; // new $ a1 (); // error // new $ a2 (); // normal // new (); // normal

So the question is, I have already introduced the namespace above. Why?new $a1()Class not found, and directnew A()Normal ?? A variable class name can be used only when all namespaces are written. The introduced namespace does not take effect. Why? I hope you can help me solve this problem.

This is the mechanism. In fact, during compilation, all classes will be converted to carry the full name of the namespace, And the namespace will disappear from the program. The variable is determined only when the program is running, so it cannot enjoy the convenience of the namespace, so it must be the full name of the class.

Namespace YourNamespace;
Use MyNamespace \ Demo;
Use MyNamespace \ Demo as MyDemo;

New Demo (); // This is correct, because use MyNamespane/Demo
New MyDemo (); // it is also true because MyNamespace/Demo as MyDemo;
New \ MyNamespace \ Demo (); // It is also correct. \ indicates the root
New MyNamespace \ Demo (); // error, which indicates new \ YourNamespace \ MyNamespace \ Demo

Understand?
New $ a1 () failed because @ has a clear explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.