PHP Namespaces: Unqualified name, qualified name, fully qualified name instance detailed

Source: Internet
Author: User
Tags php programming
In the PHP namespace, you need to know about the terms of space three names: Unqualified Name, qualified name, fully qualified name, and how PHP parses them. The official documentation is very good, just take it, and it's helpful to learn what's behind it. Before we learned about namespaces and public spaces, the invocation syntax for namespaces makes sense as a file path, allowing us to customize the subspace to describe the relationships between the spaces.

The terms for the three names in a namespace are as follows:

1. Unqualified name , or class name that does not contain a prefix, for example $comment = new comment ();. If the current namespace is blog\article,comment, it will be resolved to blog\article\comment. If the code that uses comment is not included in the code in any namespace (in global space), then comment is resolved to comment.

2. Qualify the name , or include the name of the prefix, for example $comment = new Article\comment ();. If the current namespace is a blog, Comment will be parsed as blog\article\comment. If the code that uses comment is not included in the code in any namespace (in global space), then comment is resolved to comment.

3. Fully qualified name , or contains the name of the global prefix operator, for example $comment = new \article\comment ();. In this case, Comment is always parsed into the literal name in the code (literal name) article\comment.

You can actually compare these three names to filenames (such as comment.php), relative pathname (for example,./article/comment.php), Absolute pathname (for example,/blog/article/comment.php), which may be easier to understand.

Create a blog space here, using unqualified names, representing the current blog space, the call will be parsed after instantiation. Using the qualified name, which is relative to the Blog space, the invocation will be parsed into blog\article\comment () after instantiation, notice that there is no backslash in front of the class. Using the fully qualified name, which is absolute to the blog space, after instantiation this call will be parsed, notice that the class preceded by a backslash and no backslash difference.

The sample code is as follows:

<?php//creates a space blognamespace Blog;class Comment {}//Unqualified name that represents the current Blog space//This call will be parsed into blog\comment (); $blog _comment = new Comment ();//qualified name, indicating relative to the Blog space//This call will be parsed into blog\article\comment (); $article _comment = new Article\comment (); There is no backslash \//fully qualified name in front of the class, which means it is absolutely in the Blog space//This call will be parsed into blog\comment (); $article _comment = new \blog\comment (); The class has a backslash \//fully qualified name, which means it is absolutely in the Blog space//This call will be parsed into blog\article\comment (); $article _comment = new \blog\article\comment (); class preceded by a backslash \//create a Blog subspace Articlenamespace blog\article;class Comment {}?>

In fact, the previous introduction has been using unqualified names and fully qualified names, and now they finally have a name.

The three names and terms in the namespace first know here, the next section will take friends to understand the aliases and imports in the namespace.

"Related tutorials Recommended"

1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"

2. Video Tutorial: namespaces: Although we have the same name, we belong to different time and space

3. PHP programming from getting started to mastering the full set of tutorials

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.