search
HomeBackend DevelopmentPHP Tutorialphp namespace (detailed answer combined with code)

In php## namespace, you need to know the terminology of the three names of spaces: unqualified names, qualified names, fully qualified names, and how PHP parses them. The official documents are very good, so I just used them. Understanding them is very helpful for learning the following content. We learned earlier about 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 that does not include a prefix, such as $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 name containing 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 the 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:

[html] view plain copy
<?php  
//创建空间Blog  
namespace Blog;  
class Comment { }  
//非限定名称,表示当前Blog空间  
//这个调用将被解析成 Blog\Comment();  
$blog_comment = new Comment();  
//限定名称,表示相对于Blog空间  
//这个调用将被解析成 Blog\Article\Comment();  
$article_comment = new Article\Comment(); //类前面没有反斜杆\  
//完全限定名称,表示绝对于Blog空间  
//这个调用将被解析成 Blog\Comment();  
$article_comment = new \Blog\Comment(); //类前面有反斜杆\  
//完全限定名称,表示绝对于Blog空间  
//这个调用将被解析成 Blog\Article\Comment();  
$article_comment = new \Blog\Article\Comment(); //类前面有反斜杆\  
//创建Blog的子空间Article  
namespace Blog\Article;  
class Comment { }  
?>

The above is the php namespace I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed introduction to the difference between PHP namespace and automatic loading

Detailed introduction to PHP in combination with code Scope

Detailed usage method in PHP closure function() use()

The above is the detailed content of php namespace (detailed answer combined with code). For more information, please follow other related articles on the PHP Chinese website!

Statement
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
What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

How do you redirect a page in PHP?How do you redirect a page in PHP?Apr 28, 2025 pm 04:54 PM

The article discusses various methods for page redirection in PHP, focusing on the header() function and addressing common issues like "headers already sent" errors.

Explain type hinting in PHPExplain type hinting in PHPApr 28, 2025 pm 04:52 PM

Article discusses type hinting in PHP, a feature for specifying expected data types in functions. Main issue is improving code quality and readability through type enforcement.

What is PDO in PHP?What is PDO in PHP?Apr 28, 2025 pm 04:51 PM

The article discusses PHP Data Objects (PDO), an extension for database access in PHP. It highlights PDO's role in enhancing security through prepared statements and its benefits over MySQLi, including database abstraction and better error handling.

How to create API in PHP?How to create API in PHP?Apr 28, 2025 pm 04:50 PM

Article discusses creating and securing PHP APIs, detailing steps from endpoint definition to performance optimization using frameworks like Laravel and best security practices.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.