Namespaces are the most important change in PHP 5.3, and the word is so familiar to C # and Java developers that they can finally better change the structure of the PHP application.
Why do we need namespaces?
With the growth of your PHP code base, the risk is also higher when you modify the previously defined functions and class names, and when you try to add a third party component or plug-in, what happens if you have two or more code sets that implement a "Database" and a "User" class?
Until now, the only solution is to use long class/function names, such as the prefix "Wp_" used by WordPress for each class and functor name, and the Zend framework uses a very descriptive naming convention that causes the class name to be very verbose, such as:
Zend_search_lucene_analysis_analyzer_common_text_caseinsensitive
A naming conflict problem can be resolved using namespaces, and PHP constants, classes, and functions can be grouped into a namespace library.
How do I define namespaces?
By default, all constants, classes, and function names are placed in global space, just as they were before the PHP support namespace.
Use a keyword namespace at the top of the PHP file to define the namespace, which must be the first command (except declare), before it can appear without PHP code, HTML, or spaces. Such as:
< PHP
- Define this code in the ' MyProject ' namespace
- namespace MyProject;
- ... code ...
The following code in this line is assigned to the MyProject namespace, it is not possible to nest namespaces or define multiple namespaces for the same code block, if you do, only the last namespace can be identified, but you can define different namespace codes in the same file, such as:
< PHP
- namespace MyProject1;
- PHP code for the MyProject1 namespace
- namespace MyProject2;
- PHP code for the MyProject2 namespace
- Alternative syntax
- Namespace MyProject3 {
- PHP code for the MyProject3 namespace
- }
- ?>
Although it is OK to do so, but I suggest you do not do so, it is best to define only one namespace in each file, lest you get confused.
Child namespaces
PHP allows you to define a hierarchy of namespaces so that the libraries can be subdivided, and the child namespaces are separated by a backslash character (\), such as:
Myproject\subname
Myproject\database\mysql
Companyname\myproject\library\common\widget1
Calling namespace code
In the lib1.php file we define a constant, a function, and a class using the App\lib1 namespace namespace, such as:
lib1.php
< PHP
- Application Library 1
- namespace APP\LIB1;
- Const MYCONST = ' app\lib1\myconst ';
- function MyFunction () {
- return __function__;
- }
- Class MyClass {
- static function WhoAmI () {
- return __method__;
- }
- }
- ?>
Now we can include this code in another PHP file, such as:
myapp.php
< 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";
- ?>
There is no namespace defined in myapp.php, so this code has global space, and any direct references to Myconst, MyFunction, and MyClass fail because they exist in the App\lib1 namespace. To invoke the code in lib1.php, we can add a prefix to the \APP\LIB1 namespace to define a fully qualified name, the following is the output of my load myapp.php:
App\lib1\myconst
- App\lib1\myfunction
- App\lib1\myclass::whoami
Fully qualified names can become very long, defining long names, such as App-lib1-myclass, with some obvious benefits.