How to understand the namespace concept in PHP

Source: Internet
Author: User
How to understand namespace concepts in PHP

The essence of the namespace in PHP is the container , which can act as a wrapper .

In this container we can put variables , functions , classes .


The understanding of the PHP namespace concept can be analogous to the file directory, which is used in the operating system to group related files, and it plays the role of the namespace for the files in the directory.


The root folder above contains two subfolders sub1 and SUB2,SUB1 and SUB2 are equivalent to two containers, playing the role of encapsulation, you can define the same file sub2.php under two files, because they do not interfere with each other. To refer to the contents of sub2.php in a sibling directory in the sub1.php file, include it directly (include (' sub2.php ')), However, in the sub1.php reference to sub2 under the content of the sub2.php, it is necessary to use the file delimiter, specifically, include ('. /sub2/sub2.php ').

The reference to namespaces in PHP also uses something like a file delimiter.

The actual application of the PHP namespace:

The following two issues can be resolved by namespaces:

(1) User-written code conflicts with the name of a class/function/constant or third-party class/function/constant within PHP.

(2) Create an alias for a long identifier, improve the readability of the code, and reduce the amount of code written.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

  • 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.