Design ideas and drawbacks of the PHP namespace

Source: Internet
Author: User

In contrast to languages like C #, you can arbitrarily define variables and assign values in PHP functions without worrying about overriding global variables or class variables, or you can define class variables arbitrarily, without worrying about conflicting with the function name, because there is a $ in front of the variable.

PHP's namespaces are designed like global variables and local variables, to minimize naming conflicts. PHP namespaces can be similar to file systems, with relative namespaces and absolute namespaces.

Define namespaces

Must be the first statement, preceded by no statements, comments can be.
Must be an absolute namespace.

namespace bookrpg\config;//或者namespace bookrpg\config{}namespace \bookrpg\config;  //错误,不能有前导
Import namespaces

Must be an absolute namespace.

use bookrpg\config\ClassName;use \bookrpg\config\ClassName; //可以有前导\,但不建议加use bookrpg\config\ClassName as NewClassName;use Exception;use \Exception; //可以有前导\,但不建议加
Using namespaces

Using an absolute namespace

$cls = new \bookrpg\config\ClassName();class MyException extends \Exception{}

Using relative namespaces

namespace bookrpg{    //等同于new \bookrpg\config\ClassName();    $cls = new config\ClassName();     //错误,php会认为是\bookrpg\Exception,而不是全局类Exception    $ex = new Exception(); }
Weird place.

C + +, Java, C # All have similar namespace concepts, and PHP namespaces do not follow the crowd, have their own set of independent ideas, for the preconceived people to come to appear strange and confusing.

    1. Is there a backslash in a namespace?
      Point number (.) has been used as a string connector, but also to distinguish between absolute and relative namespaces, new A.B.C () is too ugly, so choose the backslash.

    2. Once the namespace is used, the global class is also added "\" or use, tired is not tired ah?
      No way, in order to strictly analogy the concept of the file system, but also to arbitrarily define the class name, without worrying about the class name conflict.

    3. Since it is a strict analogy file system, why the definition and import of namespaces, the front does not add?
      It can only be said that the designer of PHP is not a perfectionist.

But the PHP namespace does lack logical perfection:

    • The use of leading \ is not uniform, the same is the absolute namespace, the definition of the time is not allowed, when the import is allowed.
    • Even if the global class is under a non-global namespace, leading \ must be used, and global functions and variables are not subject to this restriction.
Usage recommendations
    • When defining and importing namespaces, the unification does not add a leading \
    • All classes are imported using use, including global classes, namespace-only classes, conflicting as a
    • Prohibit direct use of relative namespaces

Understanding the design of the PHP namespace and avoiding its imperfect design makes it seem that the PHP namespace is also easy to accept.

Design ideas and drawbacks of the PHP namespace

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.