Include this file in another PHP file. Myapp. php1234567? Phpheader (Content-type: textplain); require_once (lib1.php); echoAppLib1MYCONST. n; echoAppLib1MyFunction (). n; echoAppLib1MyClass: WhoAmI (). n ;?
Include this file in another PHP file. Myapp. php 1 2 3 4 5 6 7? Php header ('content-type: text/plain '); require_once ('lib1. php '); echo \ App \ Lib1 \ MYCONST. "\ n"; echo \ App \ Lib1 \ MyFunction (). "\ n"; echo \ App \ Lib1 \ MyClass: WhoAmI (). "\ n ";?
Include this file in another PHP file.
Myapp. php
1 2 3 4 5 6 7 |
header( 'Content-type: text/plain' );
require_once ( 'lib1.php' );
echo \App\Lib1\MYCONST . "\n" ;
echo \App\Lib1\MyFunction() . "\n" ;
echo \App\Lib1\MyClass::WhoAmI() . "\n" ;
?>
|
Because no namespace is defined in myapp. php, all code is in the global space. Any direct reference to MYCONST, MyFunction, or MyClass will fail because they are in the App \ Lib1 namespace. To call the code in lib1.php, you must add the "\ App \ Lib1" prefix to form a fully qualified name (fully-qualified names ). The running result of myapp. php is:
App \ Lib1 \ MYCONST
App \ Lib1 \ MyFunction
App \ Lib1 \ MyClass: WhoAmI
A fully qualified name can also be long, and it is not significantly advantageous for long class names such as App-Lib1-MyClass. Therefore, in the next article, we will discuss namespaces and take a closer look at how PHP handles namespaces.
How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution
In the following example, two sections of code are almost identical. The only difference lies in their namespaces.
Lib1.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// application library 1
namespace App\Lib1;
const MYCONST = 'App\Lib1\MYCONST' ;
function MyFunction() {
return __FUNCTION__ ;
}
class MyClass {
static function WhoAmI() {
return __METHOD__ ;
}
}
?>
|
Lib2.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// application library 2
namespace App\Lib2;
const MYCONST = 'App\Lib2\MYCONST' ;
function MyFunction() {
return __FUNCTION__ ;
}
class MyClass {
static function WhoAmI() {
return __METHOD__ ;
}
}
?>
|
First, let's take a look at some PHP terms:
Fully Qualified name
Any PHP code has a fully qualified name-an identifier starting with a backslash. For example, \ App \ Lib1 \ MYCONST, \ App \ Lib2 \ MyFunction ().
A fully qualified name does not produce any ambiguity. The first backslash is similar to the file system path and specifies the "root" global space. If you implement a MyFunction () function in the global space, you can use "\ MyFunction ()" In lib1.php and lib2.php to call it.
Fully qualified names are helpful for one-time function calls or object initialization. However, they are not applicable to a large number of calls. As you can see next, PHP provides other options for namespace input.
Partial qualified name
There must be at least one namespace separator identifier. For example: Lib1 \ MyFunction ().
Unqualified name
There is no namespace separator identifier. Example: MyFunction ().
Work in the same namespace
Consider the following code:
Myapp1.php
1 2 3 4 5 6 7 8 9 |
namespace App\Lib1;
require_once ( 'lib1.php' );
require_once ( 'lib2.php' );
header( 'Content-type: text/plain' );
echo MYCONST . "\n" ;
echo MyFunction() . "\n" ;
echo MyClass::WhoAmI() . "\n" ;
?>
|
Although both lib1.php and lib2.php are included, MYCONST, MyFunction, and MyClass only reference the code in lib1.php. Because the code of myapp1.php is also located in the App \ lib1 namespace. Result: App \ Lib1 \ MYCONSTApp \ Lib1 \ MyFunctionApp \ Lib1 \ MyClass: WhoAmINamespace ImportThe namespace can be imported using the use operator. For example, myapp2.php
1 2 3 4 5 6 7 8 9 |
use App\Lib2;
require_once ( 'lib1.php' );
require_once ( 'lib2.php' );
header( 'Content-type: text/plain' );
echo Lib2\MYCONST . "\n" ;
echo Lib2\MyFunction() . "\n" ;
echo Lib2\MyClass::WhoAmI() . "\n" ;
?>
|
You can use any number of use statements or comma to separate each independent namespace. In this example, the "App \ Lib2" namespace is imported. However, at this moment, you still cannot directly reference MYCONST, MyFunction, or MyClass. Because the code called is in the global space, PHP will search for them in the global space. However, if we add the prefix "Lib2 \", some qualified names are formed. PHP searches the imported namespace until a match is found.
Result
App\Lib2\MYCONSTApp\Lib2\MyFunctionApp\Lib2\MyClass::WhoAmI
Namespace alias
The namespace alias is perhaps the most helpful feature. Aliases can replace long names with short names.
Myapp3.php
1 2 3 4 5 6 7 8 9 10 11 |
use App\Lib1 as L;
use App\Lib2\MyClass as Obj;
header( 'Content-type: text/plain' );
require_once ( 'lib1.php' );
require_once ( 'lib2.php' );
echo L\MYCONST . "\n" ;
echo L\MyFunction() . "\n" ;
echo L\MyClass::WhoAmI() . "\n" ;
echo Obj::WhoAmI() . "\n" ;
?>
|
The first use statement sets the alias of "App/Lib1" to "L". Some qualified names using "L" are interpreted as "App/Lib1" during compilation ". Therefore, use L \ MYCONST and L \ MyFunction to replace the fully qualified name.
The second use statement is very interesting. It sets 'obj 'to the alias of the class MyClass in the namespace of App \ Lib2. This alias setting method is only valid for classes, and constants and functions are not applicable.
After the category name is set, you can use new Obj () to create an instance or directly call the static method.
App\Lib1\MYCONSTApp\Lib1\MyFunctionApp\Lib1\MyClass::WhoAmIApp\Lib2\MyClass::WhoAmI
PHP naming rules
PHP identifier name resolution follows the following rules:
1. parse calls to constants, classes, or functions with fully qualified names during compilation.
2. undefined and partially qualified names are interpreted according to the namespace import rules. For example, if namespace A \ B \ C is imported and Its alias is set to C, then C \ D \ e () the call will be interpreted as A \ B \ C \ D \ e ()
3. In a namespace, all qualified names that are not interpreted according to the namespace import rules will have the prefix of the current namespace. For example, if C \ D \ e () is called in namespace A \ B, it will be interpretedA\B\C\D\e()。
4. The undefined class name will be interpreted based on the currently imported namespace, and the full name will be used instead of the abbreviated name. For example, if Class C in namespace A \ B is imported as X, new X () is interpretedNew A \ B \ C ().
5. Unqualified function calls in the namespace will be parsed at runtime. For example, if MyFunction () is called in namespace A \ B, PHP first searches for function \ A \ B \ MyFunction (). If no matching item is found, PHP searches for the \ MyFunction () function in the global space.
6. classes with undefined or partially qualified names will be parsed at runtime. For example, if new C () is called in namespace A \ B, PHP searches for Class A \ B \ C. If A match cannot be found, PHP will try to automatically load Class A \ B \ C.