How to Use the PHP namespace (2): import, alias, and name resolution

Source: Internet
Author: User
Address: http://www.sitepoint.com/php-namespaces-import-alias-resolution/
The first part of this series discusses why PHP namespaces are useful and the namespace keywords. This article will continue to study how to use commands and PHP resolve namespaces.
In the following example, two sections of code are almost identical. The only difference lies in their namespaces. Lib1.php
 1 <?php
2 // application library 1
3 namespace App\Lib1;
4 const MYCONST = 'App\Lib1\MYCONST';
5 function MyFunction() {
6 return __FUNCTION__;
7 }
8 class MyClass {
9 static function WhoAmI() {
10 return __METHOD__;
11 }
12 }
13 ?>

Lib2.php
 1 <?php
2 // application library 2
3 namespace App\Lib2;
4 const MYCONST = 'App\Lib2\MYCONST';
5 function MyFunction() {
6 return __FUNCTION__;
7 }
8 class MyClass {
9 static function WhoAmI() {
10 return __METHOD__;
11 }
12 }
13 ?>

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 <?php
2 namespace App\Lib1;
3 require_once('lib1.php');
4 require_once('lib2.php');
5 header('Content-type: text/plain');
6 echo MYCONST . "\n";
7 echo MyFunction() . "\n";
8 echo MyClass::WhoAmI() . "\n";
9 ?>

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:

1 App\Lib1\MYCONST
2 App\Lib1\MyFunction
3 App\Lib1\MyClass::WhoAmI

Namespace Import
The namespace can be imported using the use operator. For example
Myapp2.php

1 <?php
2 use App\Lib2;
3 require_once('lib1.php');
4 require_once('lib2.php');
5 header('Content-type: text/plain');
6 echo Lib2\MYCONST . "\n";
7 echo Lib2\MyFunction() . "\n";
8 echo Lib2\MyClass::WhoAmI() . "\n";
9 ?>
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
1 App\Lib2\MYCONST
2 App\Lib2\MyFunction
3 App\Lib2\MyClass::WhoAmI
  Namespace aliasThe namespace alias is perhaps the most helpful feature. Aliases can replace long names with short names. Myapp3.php
 1 <?php
2 use App\Lib1 as L;
3 use App\Lib2\MyClass as Obj;
4 header('Content-type: text/plain');
5 require_once('lib1.php');
6 require_once('lib2.php');
7 echo L\MYCONST . "\n";
8 echo L\MyFunction() . "\n";
9 echo L\MyClass::WhoAmI() . "\n";
10 echo Obj::WhoAmI() . "\n";
11 ?>
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. Result:
1 App\Lib1\MYCONST
2 App\Lib1\MyFunction
3 App\Lib1\MyClass::WhoAmI
4 App\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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.