Regarding the namespace, the official document has 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 problem of duplicate names. 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 which has a class Comment for processing user messages. Later, 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 their Comments, but it is obviously not possible to introduce their respective Comment classes at the same time. The code will make errors, and rewriting any Comment in another place will also reduce maintainability. At this time, I can only reconstruct the class name. I agreed on a naming rule and added the module name in front of the class name, like this: Article_Comment, MessageBoard_Comment
As you can see, the name becomes very long, which means When I use Comment in the future, I will write more code (at least more characters). 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. Both const and define are used to declare constants (their differences are not detailed), but in a namespace, define acts globally, while const acts on the current space. The constants I mentioned in the article refer to constants declared using const.
Basic:
Namespace divides the code into different spaces (areas), and the constants, functions, and classes of each space (for the sake of laziness, I They are called elements below) and their names have no influence on each other. This is somewhat similar to the concept of 'encapsulation' that we often mention.
To create a namespace, you need to use the namespace keyword
The code is as follows:
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 all wrong:
The code is as follows:
//在脚本前面写了一些逻辑代码
or as follows:
//在脚本前面输出了一些字符
Why do we need 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:
The code is as follows:
Between different spaces You cannot directly call other elements. You need to use the namespace syntax:
The code is as follows:
As you can see, when calling the Comment class in the article space in the MessageBoard space, a A syntax like a file path: \space name\element name
Except for classes, the usage of functions and constants is the same. Below I created new elements for the two spaces and placed them in the MessageBoard space. Their values are output in .
The code is as follows:
Subspace:
The calling syntax of the namespace like the file path makes sense, it allows us to customize Subspaces are used to describe the relationships between spaces.
Sorry, I forgot to mention that the two modules article and message board are actually in the same blog project. If you use namespace to express their relationship
Public space:
I have a common_inc.php script file, which has some useful functions And class:
Introducing this script in a namespace, the elements in the script will not belong to this namespace. If no other namespace is defined in this script, its elements will always be in the public space:
The way to call the public space is to directly add \ before the element name, otherwise the PHP parser will think I want to call the element under the current space. In addition to custom elements, there are also elements that come with PHP, which all belong to the public space.
I want to mention that in fact, the functions and constants in the public space can be called normally without adding \ (I don’t understand why PHP does this ), but in order to correctly distinguish elements, it is recommended to add \
3 and name terms
when calling the function. Before talking about aliases and imports, you need to know the terms for the three names of spaces. And how PHP parses them. The official documentation was very good, so I just used it.
1.非限定名称,或不包含前缀的类名称,例如 $comment = new Comment();。如果当前命名空间是Blog\Article,Comment将被解析为Blog\Article\Comment。如果使用Comment的代码不包含在任何命名空间中的代码(全局空间中),则Comment会被解析为Comment。
2.限定名称,或包含前缀的名称,例如 $comment = new Article\Comment();。如果当前的命名空间是Blog,则Comment会被解析为Blog\Article\Comment。如果使用Comment的代码不包含在任何命名空间中的代码(全局空间中),则Comment会被解析为Comment。
3.完全限定名称,或包含了全局前缀操作符的名称,例如 $comment = new \Article\Comment();。在这种情况下,Comment总是被解析为代码中的文字名(literal name)Article\Comment。
其实可以把这三种名称类比为文件名(例如 comment.php)、相对路径名(例如 ./article/comment.php)、绝对路径名(例如 /blog/article/comment.php),这样可能会更容易理解。
我用了几个示例来表示它们:
别名和导入
别名和导入可以看作是调用命名空间元素的一种快捷方式。PHP并不支持导入函数或常量。
它们都是通过使用use操作符来实现
代码如下:
我注意到,如果导入元素的时候,当前空间有相同的名字元素将会怎样?显然结果会发生致命错误。
动态调用
PHP提供了namespace关键字和__NAMESPACE__
魔法常量动态的访问元素,__NAMESPACE__可以通过组合字符串的形式来动态访问
字符串形式调用问题:
上面的动态调用的例子中,我们看到了字符串形式的动态调用方式,如果要使用这种方式要注意两个问题
1、使用""时特殊字符可能被转义
2、不会认为是限定名称
PHP在编译脚本的时候就确定了元素所在的空间,以及导入的情况。而在解析脚本时字符串形式调用只能认为是非限定名称和完全限定名称,而永远不可能是限定名称
总结
我对PHP的命名空间刚刚接触,也不能随便给一些没有实践的建议。我个人认为命名空间的作用和功能都很强大,如果要写插件或者通用库的时候再也不用担心重名问题。不过如果项目进行到一定程度,要通过增加命名空间去解决重名问题,我觉得工作量不会比重构名字少。也不得不承认它的语法会对项目增加一定的复杂度,因此从项目一开始的时候就应该很好的规划它,并制定一个命名规范。
更多相关问题请访问PHP中文网:PHP视频教程
The above is the detailed content of PHP - detailed introduction to namespace usage. For more information, please follow other related articles on the PHP Chinese website!