1. Introduce PHP namespaces
What is a namespace? In a broad sense, namespaces are a way of encapsulating things. This abstract concept can be seen in many places. For example, in an operating system, a directory is used to group related files, and for files in a directory, it plays the role of a namespace. For example, file Foo.txt can exist in both directory/home/greg and/home/other, but there cannot be two foo.txt files in the same directory. In addition, when accessing the Foo.txt file outside of directory/home/greg, we must place the directory name and directory delimiter before the file name to get/home/greg/foo.txt. the application of this principle to the field of programming is the concept of namespaces.
In PHP, namespaces are used to solve two types of problems encountered when writing a class library or application to create reusable code such as classes or functions:
User-written code conflicts with the name of a class/function/constant or third-party class/function/constant inside PHP.
Creates an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem), improving the readability of the source code.
The PHP namespace provides a way to group related classes, functions, and constants together.
2.php that version supports namespaces above?
PHP supports namespaces at the beginning of the 5.3.0 version
3. Test examples
A. Test a
The directory structure and path of the test file are as follows:
E:\myphp\research\namespace\market\fruit\inc.php
E:\myphp\research\namespace\market\fruit\inc_test1.php
E:\myphp\research\namespace\market\fruit\inc_test2.php
E:\myphp\research\namespace\market\fruit\inc_test3.php
E:\myphp\research\namespace\market\fruit\inc_test4.php
E:\myphp\research\namespace\market\fruit\inc_test5.php
E:\myphp\research\namespace\market\fruit\inc_test6.php
inc.php
1<?PHP2 /*3 * @date 2016-4-84 * @author Caihuafeng5 */6 namespaceMarket\fruit;7 classApple {8 Publicfunction __construct ($file =__file__) {9Echo sprintf ("Inner |%s|, invoked by file:|%s|\n", __method__, $file);Ten } One } A?>
inc_test1.php
1<?PHP2 /*3 * @date 2016-4-84 * @author Caihuafeng5 */6Include_once ('inc.php');7 8 //instantiate a class directly preceded by a namespace9$apple =New\market\fruit\apple (__file__);Ten One //output inner |market\fruit\apple::__construct|, invoked by file:|e:\myphp\research\namespace\market\fruit\inc_ test1.php| A - //using new Market\fruit\apple (__file__), or New \market\fruit\apple (__file__), all output the same result, that is, there are no \ results in front of the market -?>
inc_test2.php
1<?PHP2 /*3 * @date 2016-4-84 * @author Caihuafeng5 */6 namespaceMarket\fruit;//adjust the current script to market\fruit this namespace, and namespace declaration must be in the first sentence7Include_once ('inc.php');8 9 //because the first line above declares the class Apple's namespace market\fruit, the following instantiation of the class Apple does not require a namespace in front of Apple.Ten$apple =NewApple (__file__); One //output inner |market\fruit\apple::__construct|, invoked by file:|e:\myphp\research\namespace\market\fruit\inc_ test2.php| A?>
inc_test3.php
1<?PHP2 /*3 * @date 2016-4-84 * @author Caihuafeng5 */6Include_once ('inc.php');7 8 //define aliases, market\fruit\apple the last Apple is the class name, and define the Market\fruit\apple alias as MFA9Use Market\fruit\apple asMFA;Ten One //since the alias for the definition market\fruit\apple is the MA, it is possible to use MFA instead of Market\fruit\apple A$apple =NewMFA (__file__); - //output inner |market\fruit\apple::__construct|, invoked by file:|e:\myphp\research\namespace\market\fruit\inc_ test3.php| -?>
inc_test4.php
1<?PHP2 /*3 * @date 2016-4-84 * @author Caihuafeng5 */6Include_once ('inc.php');7 8 //defines an alias, Market\fruit is a namespace, and defines Market\fruit as an alias of MF9Use Market\fruit asMF;Ten //warning:the Use statement with Non-compound name ' market ' have no effect in E:\myphp\research\namespace\market\inc_te st4.php on line 5 One A //since the alias for the definition market\apple is the MA, the following can be used to replace the market\apple directly with the MA -$apple =Newmf\apple (__file__); - //output inner |market\fruit\apple::__construct|, invoked by file:|e:\myphp\research\namespace\market\fruit\inc_ test4.php| the - /* - if the above $apple = new Mf\apple (__file__), modify to $apple = new \mf\apple (__file__); [The difference is that in front of the MF plus \, if the alias is defined, then you do not need to add \] Before the alias, will be reported as the following error: - Fatal error:class ' mf\apple ' not found in E:\myphp\research\namespace\market\inc_test4.php on line 9 + */ -?>
inc_test5.php
1<?PHP2 /*3 * @date 2016-4-84 * @author Caihuafeng5 */6Include_once ('inc.php');7 8 //If you omit the following as ..., then you can use the last section of the text to fruit instead9 Use market\fruit;Ten One //since the use Market\fruit is declared above, the following instantiation is replaced with fruit market\fruit A$apple =Newfruit\apple (__file__); - //output inner |market\fruit\apple::__construct|, invoked by file:|e:\myphp\research\namespace\market\fruit\inc_ test5.php| -?>
inc_test6.php
1<?PHP2 /*3 * @date 2016-4-84 * @author Caihuafeng5 */6Include_once ('inc.php');7 8 //If you omit the following as ..., then you can directly use the last section of the text to Apple instead9 Use market\fruit\apple;Ten One //since the use Market\fruit is declared above, the following instantiation is replaced with fruit market\fruit A$apple =NewApple (__file__); - //output inner |market\fruit\apple::__construct|, invoked by file:|e:\myphp\research\namespace\market\fruit\inc_ test6.php| -?>
B. Test Two
E:\myphp\research\namespace\demo.php
1<?PHP2 /*3 * @date 2016-4-84 * @author Caihuafeng5 * The class name in the namespace is directly new under the namespace, you can omit the use syntax, which is caused by the execution of PHP in script order. For example, the following code can be run6 */7 namespacedemotest;8 classDemo {9 Publicfunction __construct () {TenEcho"This is a test script\n"; One } A } - - namespaceDemoNamespace; the classDemo { - Publicfunction __construct () { -Echo'This is namespace of PHP demo, the demo magic constant ' __namespace__ ' is'. __namespace__."\ n"; - } + } - /* + under the same script, the new one does not specify which namespace to use, and it follows the script, using the class from the namespace closest to the new statement A The following line is equivalent to $demo = new \demonamespace\demo (); at */ -$Demo =NewDemo (); - - //calling the demo class for demotest space -Use Demotest\demo asD; -$d =Newd (); in - /* to Output + This was namespace of PHP demo, the demo magic Constant "__namespace__" is DemoNamespace - This is a test script the */ * $ /*Panax Notoginseng This result shows that, under the same script, the new one does not specify which namespace to use, and then follows the script, using the class in the namespace closest to the new statement - */ the?>
Extended reading:
PHP supports namespaces at the beginning of the 5.3.0 version.
http://zhidao.baidu.com/question/401496650
PHP Use PHP namespace What's the matter?
http://zhidao.baidu.com/question/506941048
PHP namespace use include
Http://www.baidu.com/s?wd=php%20namespace%20use%20include
Namespaces in PHP (namespace) and how to use them
Http://blog.jjonline.cn/phptech/154.html
The use of PHP namespaces (Namespace) is detailed
Http://www.cnblogs.com/kuyuecs/p/3556421.html
thinkphp Use load File
Http://www.baidu.com/s?wd=thinkphp%20use%20 Loading files
PHP namespace dynamic access and usage tips
Http://www.jb51.net/article/53856.htm
PHP namespace and use issues
http://bbs.csdn.net/topics/390727877
Http://www.baidu.com/s?wd=php%20namespace
Http://www.sogou.com/web?query=php%20namespace
Http://www.sogou.com/web?query=php%20namespace
Php Namespace Namespace Usage Summary