PHP namespace: Detailed explanation of unqualified names, qualified names, and fully qualified name instances

伊谢尔伦
Release: 2023-03-07 19:28:02
Original
3503 people have browsed it

In the PHP namespace, you need to know the terminology of the three names of the space: unqualified name, qualified name, fully qualified name, and how PHP parses them. The official documents are very good, so I just used them directly. Understanding them is very helpful for learning the following content. We learned earlier that The subspace and public space of the namespace, it makes sense that the calling syntax of the namespace is like a file path. It allows us to customize the subspace to describe the relationship between each space.

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

1. Unqualified name , or Class name without prefix, for example $comment = new Comment();. If the current namespace is Blog\Article, Comment will be parsed as Blog\Article\Comment. If the code using Comment does not contain code in any namespace (in the global space), the Comment will be parsed as a Comment.

2. Qualified name , or a name containing a prefix, such as $comment = new Article\Comment();. If the current namespace is Blog, Comment will be parsed as Blog\Article\Comment. If the code using Comment does not contain code in any namespace (in the global space), the Comment will be parsed as a Comment.

3. Fully qualified name , or a name that includes a global prefix operator, such as $comment = new \Article\Comment();. In this case, Comment is always resolved to the literal name Article\Comment in the code.

In fact, these three names can be compared to file names (such as comment.php), relative path names (such as ./article/comment.php), and absolute path names (such as /blog/article/comment. php) which might be easier to understand.

Create a Blog space here, using an unqualified name to represent the current Blog space. This call will be parsed after instantiation. Use a qualified name to indicate that it is relative to the Blog space. After instantiation, this call will be parsed into Blog\Article\Comment(). Note that there is no backslash in front of the class. Use a fully qualified name, indicating that it is absolute to the Blog space. This call will be parsed after instantiation. Note the difference between a backslash in front of the class and no backslash.

The sample code is as follows:

Copy after login

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

The three names and terms in the namespace are learned here first. The next chapter will take friends to understand the aliases and imports in the namespace.

【Related tutorial recommendations】

1. "php.cn Dugu Jiujian (4)-php video tutorial"

2. Video tutorial :Namespace: Although we have the same name and the same gender, we belong to different time and space

3. Full set of tutorials on PHP programming from entry to master

The above is the detailed content of PHP namespace: Detailed explanation of unqualified names, qualified names, and fully qualified name instances. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!