A preliminary study on PHP namespace and automatic loading, and a preliminary study on php namespace. A preliminary study on PHP namespace and automatic loading; a preliminary study on php namespace reference: PHP Manual-language reference: php. netmanualzhlanguage. namespaces. php Overview: 1. declared the namespace PHP namespace and automatic loading preliminary, php namespace preliminary
Reference: PHP Manual-language reference: http://php.net/manual/zh/language.namespaces.php Overview: 1. after the namespace is declared, the following const, function, class will be assigned to the namespace. 2. only PHP files with namespaces declared can load PHP files with namespaces. 3. PHP 5.3 and above can use namespace nouns: Keyword: NAMESPACE is used to declare the namespace constant of this PHP file: __namespace _ to return the name of the current NAMESPACE is a null string by default.
Operator: use uses the last \ string as the alias by default, and uses the as the alias, which is consistent with the MySQL field alias.
The actual operation is as follows:Create the following file in the apache Directory: index. php, Order. php, and User. php Order. php:
1
'; 10 11 function detail () 12 {13 return' order detail
'; 14} 15 function call_by_self () 16 {17 return 'call by self
'; 18} 19/** 20*21 */22 class Orderlist23 {24 25 public function _ construct () 26 {27 echo 'class NameSpace is "', _ NAMESPACE __, '"'; 28} 29 public function show_list () 30 {31 for ($ I = 0; $ I <5; $ I ++) {32 echo"
- This is order $ I
"; 33 // access 34 echo detail (); 35 echo internally directly"
"; 36} 37} 38} 39 // internally access 40 echo \ Order \ call_by_self () through the namespace ();
Index. php content:
1
Show_list (); 14 15 // externally accessed static variables and functions directly access 16 use Order; 17 echo Order \ STR; 18 echo Order \ detail ();
The output is as follows:
The above content includes: Access files through namespaces, direct instantiation access, and direct access to this space. The namespace is used to prevent two classes with the same name from being loaded. namespace can avoid conflicts of the same name when loading a third-party class. Next, the full name of the automatic loading SPL is: Standard PHP Library PHP Standard Library, which has been built into PHP after PHP5, and does not need to be installed separately. SPL contains a set of function libraries for data structures, iterators, exceptions, and File Processing.
The auto-load library has the following functions:
Spl_autoload_call: Call all registered _ autoload () functions to load the request class.
User. php content:
1 namespace User; 2 // directly load Order3 # include ('order. php '); 4 // automatically load 5 spl_autoload_register (function ($ className) {6 var_dump ($ className); 7}); 8 spl_autoload_call ('order ');
The output is as follows:
The SPL automatic loading function includes the following:
Spl_autoload_extensions: registers and returns the default file extension used by the spl_autoload function.
Get_include_path: set the default referenced folder
Spl_autoload_register: automatically introduces files
The actual operation is as follows:We re-adjusted the directory structure and copied order to lib as follows:
Modify User. php as follows:
1 namespace User; 2 3 // directly load Order 4 # include ('order. php '); 5 // automatically load 6 define ('Lib _ dir', _ DIR __. DIRECTORY_SEPARATOR. 'Lib '. DIRECTORY_SEPARATOR); 7 spl_autoload_register (function ($ class) {8 $ path = LIB_DIR. $ class. '. lib. php '; 9 include ($ path); 10}); 11 12 spl_autoload_call ('order'); 13 use Order; 14 15 $ orderList = new \ Order \ Orderlist (); 16 $ orderList-> show_list ();
The output is as follows:
Note:When the SPL is used to load files, use does not trigger the spl_autoload_register function. it will be triggered by new, so that the file cannot be found. all spl_autoload_call is used to trigger automatic loading in advance. Address: http://www.cnblogs.com/martin-tan/p/4864539.html
Problem:If get_include_path, spl_autoload_extensions, and spl_autoload_register are empty by default, files in the directory cannot be directly loaded, as shown in the preceding figure. (?)
Refer reference: PHP Manual-language reference: http://php.net/manual/zh/language.namespaces.php Overview: 1. declared the namespace...