New features of modern PHP series (1) -- namespace
1. what is a namespace?
If you only need to know one of the modern PHP features, it should be a namespace. Namespace is introduced in PHP5.3.0 to organize PHP code based on a virtual hierarchy, which is similar to the directory structure of the file system in the operating system. Namespace is the foundation of the modern PHP component ecosystem. the code of the modern PHP component framework is placed in the namespace of the vendor that is globally unique to avoid conflicts with common class names used by other vendors.
Next let's take a look at how the real PHP component uses the namespace. The Http component in the Laravel framework is used to manage HTTP requests and responses. this component uses common class names, such as Request and Response. many other PHP components also use this class name, since other PHP code uses the same class name, how can we use this component? In fact, we can safely use this component, because the code of this component is placed in the only vendor namespace Illuminate. Open this component repository (https://github.com/laravel/framework/blob/master/src/Illuminate/Http/Response.php) in GitHub and find the Response. php file:
The code for Line 1 is as follows:
namespace Illuminate\Http;
This line is a PHP namespace declaration statement. the code for declaring the namespace should always be placed in
The role of a namespace is to encapsulate PHP classes related to the organization, just like placing related files in the same directory in the file system. The PHP namespace is different from the physical file system of the operating system. it is a virtual concept and does not need to be identical to the directory structure in the file system, however, most PHP components put namespaces in subdirectories of the corresponding file system to be compatible with widely used PSR-4 automatic loading standards.
2. Why do I use namespaces?
As mentioned above, our code may use the same class name, interface name, function or constant name as other developers' code. if namespace is not used, the name may conflict, PHP execution error. Using a namespace to place code in a unique vendor namespace, our code can use the same class name, interface name, function or constant name as other developers.
Of course, if you develop small personal projects with only a small number of dependencies, class name conflicts may not be a problem. However, if you work in a team, you will need to develop large projects that are dependent on many third parties, take naming conflicts seriously, because you cannot control the classes, interfaces, functions, and constants introduced by project dependencies in the global namespace. This is why namespaces are used.
3. declare a namespace
Every PHP class, interface, function, and constant are in the namespace. it is easy to declare the namespace.
The namespace is often used to set the top-level vendor name. for example, we set the vendor name to LaravelAcademy:
All PHP classes, interfaces, functions, and constants declared after the namespace statement are in the LaravelAcademy namespace and have some relationship with Laravel Institute. What should we do if we want to organize the code used by the school? The answer is to use a sub-namespace.
The sub-namespace is declared in the same way as the preceding example. The only difference is that we need to use the \ symbol to separate the namespace from the sub-namespace. for example:
All classes, interfaces, functions, and constants in this namespace are located in LaravelAcademy \ ModernPHP.
Classes in the same namespace do not need to be declared in the same PHP file. you can specify a namespace or sub-namespace at the top of the PHP file, the code of this file is part of the namespace or sub-namespace. Therefore, you can write multiple classes in the same namespace in different files.
Note: the vendor namespace is the top-level namespace and the most important namespace. it must be globally unique to identify brands or organizations. Sub-namespaces are less important, but can be used to organize project code.
4. import and alias Before the namespace appeared, PHP developers used Zend class names to solve naming conflicts. this is a type of naming scheme, which is popular due to the Zend Framework, this naming scheme uses underscores (_) in the PHP class name to represent the directory separator of the file system. This convention has two roles: first, ensure that the class name is unique; second, the native auto-loader replaces the underline in the class name with the directory separator of the file system, to determine the file path. For example, the file corresponding to the Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query class is Zend/Cloud/DocumentService/Adapter/WindowsAzure/Query. php. We can see that this kind of naming has a disadvantage: the class name is particularly long.
The modern PHP namespace also has this problem. for example, the complete full name of the Response class is Illuminate \ Http \ Response. Fortunately, we can change this situation by importing and creating aliases.
Import indicates the namespace, class, interface, function, and constant that PHP wants to use in each PHP file. after import, the full name is not used:
send();
We use the use keyword to tell PHP that we want to use the Illuminate \ Http \ Response class. we only need to enter a fully qualified class name and then instantiate the Response, you do not need to use the complete class name.
If you think the class name is still long, you can create an alias. Creating an alias tells PHP that I want to use a simple name to reference the imported class, interface, function, or constant:
send();
You can also import functions and constants from PHP 5.6, but you need to adjust the syntax of the use keyword. to import a function, use func:
To import constants, use constant:
Aliases are also supported. The Creation method is the same as that of the class.
5. practical skills Multiple import
If you want to import multiple classes, interfaces, functions, or constants to the PHP file, you must use multiple use statements at the top of the PHP file, PHP supports writing multiple use statements into one line using a short syntax:
However, it is recommended that you do not write this statement for readability. it is better to write a use statement in one line:
One file uses multiple namespaces
PHP allows defining multiple namespaces in a file:
However, this is not a good practice and violates the "one file and one class" practice. Therefore, we do not recommend this.
Global namespace
If no namespace is specified for the referenced classes, interfaces, functions, and constants, PHP assumes that the referenced classes, interfaces, functions, and constants are in the current namespace. If you want to use a class, interface, function, or constant of another namespace, you need to use a fully qualified PHP class name (namespace + class name ).
Some code does not have a namespace in the global namespace, such as the native Exception class. When referencing global code in a namespace, you must add the \ symbol before the class, interface, function, or constant:
Automatic loading
The namespace also laid a solid foundation for PHP-FIG auto-loading standards, which are used by most modern PHP components to automate dependencies for add-ons using dependency manager Composer, in the future we will also detail the Composer and PHP-FIG, now you just need to know that there is no namespace, there is no modern PHP ecosystem and a new component-based architecture, we can see the importance of namespace.