Namespaces actually appeared as early as PHP5.3. However, most students may only be exposed to the content of namespaces in the use of various frameworks. Of course, modern development is also inseparable from these frameworks that can produce quickly. This time, we will not analyze the concept and use of namespaces from the perspective of the framework, but only from the perspective of simple code.
First, we need to define what a namespace is.
In fact, just like the directory of the operating system, the namespace is to solve the problem that the same folder cannot have the same file name in the operating system. Assuming we only have one file and one directory, then there cannot be two identical files in this directory. If there is a file with exactly the same name, the operating system does not know which file we should open. Similarly, in a PHP file, we cannot give a function or class name with the same name, and PHP does not know which function or class we are going to call.
After understanding the above content, let’s look at the syntax of the namespace, which is actually very similar to the definition of our directory.
namespace A\B\C;
The definition of this namespace is to point out that the current namespace is A\B\C. Just like a folder like C:\A\B\C. Just talking but not practicing fake styles, just put the code to see:
// namespace FILE1; const CONST_A = 2; function testA(){ echo 'FILE1\testA()', PHP_EOL; } class objectA{ function test(){ echo 'FILE1\ObjectA', PHP_EOL; } }
// namespace FILE2; const CONST_A = 3; function testA(){ echo 'FILE2\testA()', PHP_EOL; } class objectA{ function test(){ echo 'FILE2\ObjectA', PHP_EOL; } }
We created these two php files in the namespace directory. The functions and class names are the same, but different namespaces are defined, one is FILE1 and the other is FILE2.
namespace A; include 'namespace/'; include 'namespace/'; use FILE1, FILE2; use FILE1\objectA as objectB; const CONST_A = 1; function testA(){ echo 'A\testA()', PHP_EOL; } class objectA{ function test(){ echo 'A\ObjectA', PHP_EOL; } } // Current namespaceecho CONST_A, PHP_EOL; // 1 testA(); // A\testA() $oA = new objectA(); $oA->test(); // A\ObjectA // FILE1 echo FILE1\CONST_A, PHP_EOL; // 2 FILE1\testA(); // FILE1\testA() $oA = new FILE1\objectA(); $oA->test(); // FILE1\ObjectA $oB = new objectB(); $oB->test(); // FILE1\ObjectA // FILE2 echo FILE2\CONST_A, PHP_EOL; // 3 FILE2\testA(); // FILE2\testA() $oA = new FILE2\objectA(); $oA->test(); // FILE2\ObjectA
In the test code, we define the current namespace as A. and include and. And in this file, the same function and class names are also defined. Next we call these static variables, functions, and classes in turn.
- By default, static variables, functions, and classes call the contents under the current namespace
- After using FILE1\ and FILE2\, the contents under the specified namespace are called
- You need to use use to introduce the namespace, otherwise you cannot use the content in the namespace
- In use, you can use the as keyword to specify an alias for the namespace or the class in it.
The use of namespaces is actually that simple. It can be seen that we can use the same function or class name in different namespaces. This is the basis of various modern development frameworks. It is also one of the main reasons why composer can be implemented.
Next, we try to see if a question meets our expectations, that is, can two files define the same namespace to define the same class name?
// namespace FILE1; const CONST_A = 1.1; function testA(){ echo 'FILE1-1\testA()', PHP_EOL; } class objectA{ function test(){ echo 'FILE1-1\ObjectA', PHP_EOL; } }
We defined one and used the same FILE1 namespace. Then include it in the test code together.
include 'namespace/'; include 'namespace/'; // Cannot redeclare FILE1\testA()
Well, an error is reported directly at runtime, and the function name with the same name cannot be repeatedly defined. If the function is commented out, the class name will continue to be reported and cannot be repeated. Let's define another one. This time we will use the FILE1 namespace, but the content is different.
// namespace FILE1; const CONST_A = 1.2; function testA1_2(){ echo 'FILE1-2\testA()', PHP_EOL; } class objectA1_2{ function test(){ echo 'FILE1-2\ObjectA', PHP_EOL; } }
Of course there is no problem. These two files are in the same namespace, but have different abilities, which is a completely OK operation.
include 'namespace/'; include 'namespace/'; use FILE1; // FILE1 echo FILE1\CONST_A, PHP_EOL; // 2 FILE1\testA(); // FILE1\testA() $oA = new FILE1\objectA(); $oA->test(); // FILE1\ObjectA // FILE1_2 echo FILE1\CONST_A, PHP_EOL; // 3 FILE1\testA1_2(); // FILE1-2\testA() $oA = new FILE1\objectA1_2(); $oA->test(); // FILE1-2\ObjectA
Test code:/zhangyue050…
The above is a detailed explanation of the namespace in PHP. For more information about PHP namespace, please follow my other related articles!