search
HomeBackend DevelopmentPHP TutorialA preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

Preliminary exploration of PHP namespace (Namespace), namespace namespace

After exploring closures [View], let’s explore namespace again.

For namespaces, the official documents have already explained it in detail [View]. I have made some practice and summary here.

One of the clearest purposes of namespaces is to solve the duplicate name problem. PHP does not allow two functions or classes to have the same name, otherwise a fatal error will occur. In this case, it can be solved as long as you avoid naming duplication. The most common way is to agree on a prefix.

Example: There are two modules in the project: article and message board, each of them has a class Comment for processing user messages. In the future, I may want to add some information statistics functions for all user messages. For example, I want to get the number of all messages. At this time, it is a good idea to call the methods provided by them Comment, but it is obviously not possible to introduce the respective Comment classes at the same time. The code will go wrong and any one will be rewritten in another place. Comment will also reduce maintainability. Then I can only refactor the class name. I agreed on a naming rule, adding the module name in front of the class name, like this: Article_Comment, MessageBoard_Comment

As you can see, the name has become very long, which means that more code (at least more characters) will be written when using Comment in the future. Moreover, if you want to add more integration functions to each module in the future, or call each other, you will need to reconstruct the names when duplicate names occur. Of course, this problem can be avoided by noticing this problem at the beginning of the project and specifying naming rules. Another solution could be to use namespaces.

Note:

Constants mentioned in this article: Starting from PHP5.3, the const keyword can be used outside the class. const and define are both used to declare constants (their differences are not detailed), but in the namespace, the role of define is global. And const acts on the current space. The constants I mentioned in the article refer to constants declared using const.

Basics

The

namespace divides the code into different spaces (regions). The names of the constants, functions, and classes in each space (to be lazy, I will call them elements below) do not affect each other. This is somewhat similar to the concept of 'encapsulation' that we often mention.

To create a namespace, use the namespace keyword, like this:

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<?<span>php

</span><span>//</span><span>创建一个名为'Article'的命名空间</span>
<span>namespace Article;

</span>?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

It should be noted that there cannot be any code in front of the first namespace of the current script file. The following writing methods are wrong:

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<span>//</span><span>例一
//在脚本前面写了一些逻辑代码</span>

<?<span>php

</span><span>$path</span> = "/"<span>;

</span><span>class</span><span> Comment { }

namespace Article;

</span>?>
<br />

<span>//</span><span>例二
//在脚本前面输出了一些字符</span>

<html></html>
<?<span>php

namespace Article;

</span>?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

Why do you want to say the first namespace? Because multiple namespaces can be created in the same script file.

Below I created two namespaces and added a Comment class element to each of these two spaces:

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<?<span>php

</span><span>//</span><span>创建一个名为'Article'的命名空间</span>
<span>namespace Article;

</span><span>//</span><span>此Comment属于Article空间的元素</span>
<span>class</span><span> Comment { }



</span><span>//</span><span>创建一个名为'MessageBoard'的命名空间</span>
<span>namespace MessageBoard;

</span><span>//</span><span>此Comment属于MessageBoard空间的元素</span>
<span>class</span><span> Comment { }
</span>?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

You cannot directly call other elements between different spaces. You need to use the namespace syntax:

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<?<span>php

namespace Article;

</span><span>class</span><span> Comment { }



namespace MessageBoard;

</span><span>class</span><span> Comment { }

</span><span>//</span><span>调用当前空间(MessageBoard)的Comment类</span>
<span>$comment</span> = <span>new</span><span> Comment();

</span><span>//</span><span>调用Article空间的Comment类</span>
<span>$article_comment</span> = <span>new</span><span> \Article\Comment();

</span>?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

As you can see, when calling the Comment class in the article space in the MessageBoard space, a syntax like a file path is used: Space name element name

Except for classes, the usage of functions and constants is the same. Below I create new elements for the two spaces and output their values ​​in the MessageBoard space.

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<?<span>php

namespace Article;

</span><span>const</span> PATH = '/article'<span>;

</span><span>function</span><span> getCommentTotal() {
    </span><span>return</span> 100<span>;
}

</span><span>class</span><span> Comment { }




namespace MessageBoard;

</span><span>const</span> PATH = '/message_board'<span>;

</span><span>function</span><span> getCommentTotal() {
    </span><span>return</span> 300<span>;
}

</span><span>class</span><span> Comment { }

</span><span>//</span><span>调用当前空间的常量、函数和类</span>
<span>echo</span> PATH; <span>//</span><span>/message_board</span>
<span>echo</span> getCommentTotal(); <span>//</span><span>300</span>
<span>$comment</span> = <span>new</span><span> Comment();

</span><span>//</span><span>调用Article空间的常量、函数和类</span>
<span>echo</span> \Article\PATH; <span>//</span><span>/article</span>
<span>echo</span> \Article\getCommentTotal(); <span>//</span><span>100</span>
<span>$article_comment</span> = <span>new</span><span> \Article\Comment();

</span>?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

然后我的确得到了Article空间的元素数据。

 

 

子空间

命名空间的调用语法像文件路径一样是有道理的,它允许我们自定义子空间来描述各个空间之间的关系。

抱歉我忘了说,articlemessage board这两个模块其实都是处于同一个blog项目内。如果用命名空间来表达它们的关系,是这样:

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<?<span>php

</span><span>//</span><span>我用这样的命名空间表示处于blog下的article模块</span>
<span>namespace Blog\Article;

</span><span>class</span><span> Comment { }



</span><span>//</span><span>我用这样的命名空间表示处于blog下的message board模块</span>
<span>namespace Blog\MessageBoard;

</span><span>class</span><span> Comment { }

</span><span>//</span><span>调用当前空间的类</span>
<span>$comment</span> = <span>new</span><span> Comment();

</span><span>//</span><span>调用Blog\Article空间的类</span>
<span>$article_comment</span> = <span>new</span><span> \Blog\Article\Comment();

</span>?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

而且,子空间还可以定义很多层次,比如说 Blog\Article\Archives\Date

 

 

公共空间

我有一个common_inc.php脚本文件,里面有一些好用的函数和类:

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<?<span>php

</span><span>function</span><span> getIP() { }

</span><span>class</span><span> FilterXSS { }

</span>?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

 

在一个命名空间里引入这个脚本,脚本里的元素不会归属到这个命名空间。如果这个脚本里没有定义其它命名空间,它的元素就始终处于公共空间中:

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<?<span>php

namespace Blog\Article;

</span><span>//</span><span>引入脚本文件</span>
<span>include</span> './common_inc.php'<span>;

</span><span>$filter_XSS</span> = <span>new</span> FilterXSS(); <span>//</span><span>出现致命错误:找不到Blog\Article\FilterXSS类</span>

<span>$filter_XSS</span> = <span>new</span> \FilterXSS(); <span>//</span><span>正确</span>


?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

 

调用公共空间的方式是直接在元素名称前加 \ 就可以了,否则PHP解析器会认为我想调用当前空间下的元素。除了自定义的元素,还包括PHP自带的元素,都属于公共空间。

要提一下,其实公共空间的函数和常量不用加 \ 也可以正常调用(不明白PHP为什么要这样做),但是为了正确区分元素,还是建议调用函数的时候加上 \ 

 

 

名称术语

在说别名和导入之前,需要知道关于空间三种名称的术语,以及PHP是怎样解析它们的。官方文档说得非常好,我就直接拿来套了。

 

其实可以把这三种名称类比为文件名(例如 comment.php)、相对路径名(例如 ./article/comment.php)、绝对路径名(例如 /blog/article/comment.php),这样可能会更容易理解。

我用了几个示例来表示它们:

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<?<span>php

</span><span>//</span><span>创建空间Blog</span>
<span>namespace Blog;

</span><span>class</span><span> Comment { }

</span><span>//</span><span>非限定名称,表示当前Blog空间
//这个调用将被解析成 Blog\Comment();</span>
<span>$blog_comment</span> = <span>new</span><span> Comment();

</span><span>//</span><span>限定名称,表示相对于Blog空间
//这个调用将被解析成 Blog\Article\Comment();</span>
<span>$article_comment</span> = <span>new</span> Article\Comment(); <span>//类</span><span>前面没有反斜杆\

//完全限定名称,表示绝对于Blog空间
//这个调用将被解析成 Blog\Comment();</span>
<span>$article_comment</span> = <span>new</span> \Blog\Comment(); <span>//类</span><span>前面有反斜杆\

//完全限定名称,表示绝对于Blog空间
//这个调用将被解析成 Blog\Article\Comment();</span>
<span>$article_comment</span> = <span>new</span> \Blog\Article\Comment(); <span>//类</span><span>前面有反斜杆\



//创建Blog的子空间Article</span>
<span>namespace Blog\Article;

</span><span>class</span><span> Comment { }


</span>?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

 

其实之前我就一直在使用非限定名称和完全限定名称,现在它们终于可以叫出它们的名称了。

 

别名和导入

别名和导入可以看作是调用命名空间元素的一种快捷方式。PHP并不支持导入函数或常量。

它们都是通过使用use操作符来实现:

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<?<span>php

namespace Blog\Article;

</span><span>class</span><span> Comment { }



</span><span>//创建一个BBS空间(我有打算开个论坛)</span>
<span>namespace BBS;

</span><span>//</span><span>导入一个命名空间</span>
<span>use</span><span> Blog\Article;
</span><span>//</span><span>导入命名空间后可使用限定名称调用元素</span>
<span>$article_comment</span> = <span>new</span><span> Article\Comment();

</span><span>//</span><span>为命名空间使用别名</span>
<span>use</span> Blog\Article <span>as</span><span> Arte;
</span><span>//</span><span>使用别名代替空间名</span>
<span>$article_comment</span> = <span>new</span><span> Arte\Comment();

</span><span>//</span><span>导入一个类</span>
<span>use</span><span> Blog\Article\Comment;
</span><span>//</span><span>导入类后可使用非限定名称调用元素</span>
<span>$article_comment</span> = <span>new</span><span> Comment();

</span><span>//</span><span>为类使用别名</span>
<span>use</span> Blog\Article\Comment <span>as</span><span> Comt;
</span><span>//</span><span>使用别名代替空间名</span>
<span>$article_comment</span> = <span>new</span><span> Comt();

</span>?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

 

我注意到,如果导入元素的时候,当前空间有相同的名字元素将会怎样?显然结果会发生致命错误。

例:

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<?<span>php

namespace Blog\Article;

</span><span>class</span><span> Comment { }



namespace BBS;

</span><span>class</span><span> Comment { }

</span><span>Class</span><span> Comt { }


</span><span>//</span><span>导入一个类</span>
<span>use</span><span> Blog\Article\Comment;
</span><span>$article_comment</span> = <span>new</span> Comment(); <span>//</span><span>与当前空间的Comment发生冲突,程序产生致命错误

//为类使用别名</span>
<span>use</span> Blog\Article\Comment <span>as</span><span> Comt;
</span><span>$article_comment</span> = <span>new</span> Comt(); <span>//</span><span>与当前空间的Comt发生冲突,程序产生致命错误</span>

?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

 

 

动态调用

PHP提供了namespace关键字和__NAMESPACE__魔法常量动态的访问元素,__NAMESPACE__可以通过组合字符串的形式来动态访问:

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<?<span>php

namespace Blog\Article;

</span><span>const</span> PATH = '/Blog/article'<span>;

</span><span>class</span><span> Comment { }


</span><span>//</span><span>namespace关键字表示当前空间</span>
<span>echo</span> namespace\PATH; <span>//</span><span>/Blog/article</span>
<span>$comment</span> = <span>new</span><span> namespace\Comment();

</span><span>//</span><span>魔法常量__NAMESPACE__的值是当前空间名称</span>
<span>echo</span> __NAMESPACE__; <span>//</span><span>Blog\Article
//可以组合成字符串并调用</span>
<span>$comment_class_name</span> = __NAMESPACE__ . '\Comment'<span>;
</span><span>$comment</span> = <span>new</span> <span>$comment_class_name</span><span>();

</span>?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

 

字符串形式调用问题

上面的动态调用的例子中,我们看到了字符串形式的动态调用方式,如果要使用这种方式要注意两个问题。

1. 使用双引号的时候特殊字符可能被转义

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<?<span>php

namespace Blog\Article;

</span><span>class</span><span> name { }

</span><span>//</span><span>我是想调用Blog\Article\name</span>
<span>$class_name</span> = __NAMESPACE__ . "\name"; <span>//</span><span>但是\n将被转义为换行符</span>

<span>$name</span> = <span>new</span> <span>$class_name</span>(); <span>//</span><span>发生致命错误</span>

?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

 

2. 不会认为是限定名称

PHP在编译脚本的时候就确定了元素所在的空间,以及导入的情况。而在解析脚本时字符串形式调用只能认为是非限定名称和完全限定名称,而永远不可能是限定名称。

A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial
<?<span>php

namespace Blog;

</span><span>//</span><span>导入Common类</span>
<span>use</span><span> Blog\Article\Common;
</span><span>//</span><span>我想使用非限定名称调用Blog\Article\Common</span>
<span>$common_class_name</span> = 'Common'<span>;
</span><span>//</span><span>实际会被当作非限定名称,也就表示当前空间的Common类,但我当前类没有创建Common类</span>
<span>$common</span> = <span>new</span> <span>$common_class_name</span>(); <span>//</span><span>发生致命错误:Common类不存在

//我想使用限定名称调用Blog\Article\Common</span>
<span>$common_class_name</span> = 'Article\Common'<span>;
</span><span>//</span><span>实际会被当作完全限定名称,也就表示Article空间下的Common类,但我下面只定义了Blog\Article空间而不是Article空间</span>
<span>$common</span> = <span>new</span> <span>$common_class_name</span>(); <span>//</span><span>发生致命错误:Article\Common类不存在</span>
<span>

namespace Blog\Article;

</span><span>class</span><span> Common { }

</span>?>
A preliminary exploration of PHP namespace (Namespace), namespace namespace_PHP tutorial

 

 

总结

我对PHP的命名空间刚刚接触,也不能随便给一些没有实践的建议。我个人认为命名空间的作用和功能都很强大,如果要写插件或者通用库的时候再也不用担心重名问题。不过如果项目进行到一定程度,要通过增加命名空间去解决重名问题,我觉得工作量不会比重构名字少。也不得不承认它的语法会对项目增加一定的复杂度,因此从项目一开始的时候就应该很好的规划它,并制定一个命名规范。

 

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1129454.htmlTechArticlePHP命名空间(Namespace)初探,命名空间namespace 探完闭包[查看],再探命名空间。 对于命名空间,官方文档已经说得很详细[查看],我在这里...
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment