A preliminary study of PHP namespaces and automatic loading

Source: Internet
Author: User
Tags spl

Reference: PHP Manual-language Reference: http://php.net/manual/zh/language.namespaces.php Overview: 1. After the namespace is declared, the following const, function, and class are grouped into the namespace. 2. Only PHP files that declare namespaces can load PHP files with namespaces. 3. PHP 5.3 and above to use namespace nouns:Keyword: namespace namespace used to declare this PHP fileconstants:__NAMESPACE__ the name used to return the current namespace defaults to an empty string
operator: Use defaults to the string after the last \ followed by the as followed by the As string, consistent with the MySQL field alias.  The actual operation is as follows:In the Apache directory, create the following file: index.php,order.php,user.php order.php content is
1<?PHP2 /**3 * @Author: Martin4 * @Support: Martin5 * @Last Modified by:martin6  */7 namespace Order;8 9 ConstSTR = ' Order list<br/> ';Ten  One functiondetail () A { -     return' Order detail<br/> '; - } the functioncall_by_self () - { -     return' Call by Self<br/> '; - } + /** -  * +  */ A classorderlist at { -  -      Public function__construct () -     { -         Echo' Class NameSpace is ', __namespace__, ' "'; -     } in      Public functionshow_list () -     { to          for($i= 0;$i< 5;$i++) { +             Echo"<ul><li>this is Order$i<br/> "; -             //Internal Direct Access the             Echodetail (); *             Echo"</li></ul>"; $         }Panax Notoginseng     } - } the //internal access through the namespace + Echo\order\call_by_self ();

index.php content is:

1<?PHP2 /**3 * @Author: Martin4 * @Support: Martin5 * @Last Modified by:martin6  */7 namespace Index;8 include_once(' order.php ');9 Ten //external Access class instantiation can be used One  Useorder\orderlist; A $orderlist=Neworderlist; - $orderlist-show_list (); -  the //external access static variables and function direct Access -  UseOrder; - EchoOrder\str; - EchoOrder\detail ();

Printing results are:

The above includes: access to files through namespaces and direct instantiation of access, as well as direct access to this space. The namespace exists to prevent two classes with the same name from being loaded, and namespaces can be used to avoid collisions with the same name when loading a third-party class. Here's the full name of the auto-load SPL: Standard PHP library PHP libraries, built into PHP after PHP5, do not need to be installed separately. SPL contains a set of libraries for data structures, iterators, exceptions, file processing, and so on. the auto-load library has the following functions Spl_autoload_call: Attempt to call all registered __autoload () functions to load the request class

user.php content is:

1 namespace User; 2 // load Order directly 3 # include (' order.php '); 4 //Auto Load 5 spl_autoload_register (function($className) {6     Var_dump ($className); 7 }); 8 spl_autoload_call (' Order ');

Printing results are:

The SPL auto-loading function contains the following:

Spl_autoload_extensions: Registers and returns the default file name extension used by the Spl_autoload function.
Get_include_path: Set the default referenced folder
Spl_autoload_register: Automatic introduction of files
The actual operation is as follows:we re-adjust the directory structure and copy the order to Lib as follows:

Modify the user.php as follows:

1 namespace User;2 3 //load Order directly4 #include (' order.php ');5 //Auto Load6 Define(' Lib_dir ', __dir__. Directory_separator. ' Lib '.directory_separator);7Spl_autoload_register (function($class) {8     $path= Lib_dir.$class. '. lib.php ';9     include($path);Ten }); One  ASpl_autoload_call (' Order '); -  UseOrder; -  the $orderList=New\order\orderlist (); - $orderList->show_list ();

Printing results are:

Note:When using SPL to load a file, use does not trigger the Spl_autoload_register function, he will be triggered by the new, which will prompt the file cannot be found, all using Spl_autoload_call to trigger the automatic loading in advance. This address: http://www.cnblogs.com/martin-tan/p/4864539.html Questions:UseGet_include_path,spl_autoload_extensions andSpl_autoload_register The default is empty, it is not possible to load files directly into the directory, for the reasons above. (? )

A preliminary study of PHP namespaces and automatic loading

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.