One of the clearest functions of the php namespace is to solve the problem of duplicate names. In PHP, two functions or classes are not allowed to have the same name, otherwise a fatal error will occur. The previous chapter introduced What is the PHP namespace. The PHP official website has clearly defined and explained it visually. Here is a paragraph copied directly from the PHP official website: In a broad sense, namespace is a method of encapsulating things. This abstract concept can be found in many places. For example, directories are used in operating systems to group related files, and they act as namespaces for the files in the directory. For example, the file foo.txt can exist in the directories /home/greg and /home/other at the same time, but two foo.txt files cannot exist in the same directory. Additionally, when accessing the foo.txt file outside the directory /home/greg, we must put the directory name and directory separator before the file name to get /home/greg/foo.txt. The application of this principle to the field of programming is the concept of namespace.
The introduction of the php namespacenamespace keyword is to solve various "trouble" that have occurred in the php object-oriented programming process; the specific troubles are as follows:
Name conflicts between user-written code and PHP internal classes/functions/constants or third-party classes/functions/constants.
In order to alleviate trouble 1, when writing various classes, longer class names are usually used or name prefixes (or suffixes) are added to classes that implement different functions.
When the magic function __autoload is not used, and each class occupies an exclusive PHP file, in order to call different classes, other PHP files using these classes will be used. Write more include (or require or require_once) statements at the beginning.
We first create a namespace. Multiple namespaces can be created in the same script file.
The code is as follows:
1 2 3 4 5 6 7 8 9 10 |
|
You cannot directly call other elements between different spaces, you need to use the namespace syntax.
The sample code is as follows:
1 2 3 4 5 6 7 8 9 10 |
|
You can see that 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 created new elements for the two spaces and output them in the MessageBoard space. value.
The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
The following results will be obtained:/message_board300/article100
[Related tutorial recommendations]
1. "php.cn Dugu Jiujian (4)-php video tutorial"
2. Video tutorial: naming Space: Although we have the same name and the same gender, we belong to different time and space
3. A complete set of tutorials on PHP programming from entry to master
The above is the detailed content of Detailed explanation of the role of php namespace. For more information, please follow other related articles on the PHP Chinese website!