A preliminary study of PHP namespaces and automatic loading, PHP namespaces _php Tutorial

Source: Internet
Author: User
Tags spl

A preliminary study of PHP namespaces and automatic loading, a preliminary study of PHP namespaces


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 file constants:__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 '
';Ten One functiondetail () A { - return' Order Detail
'; - } the functioncall_by_self () - { - return' Call to Self
'; - } + /** - * + */ A classorderlist at { - - Public function__construct () - { - Echo' Class NameSpace is ', __namespace__, ' "'; - } in Public functionshow_list () - { to for($i= 0;$i< 5;$i++) { + Echo"
    • This is Order$i
      "; - // Internal Direct Access the Echo detail (); * Echo "
"; $ }Panax Notoginseng } - } the //internal access through the namespace + Echo\order\call_by_self ();

index.php content is:

 
 1 
   PHP 2/* *  3  * @Author: Martin  4  * @Support: Martin  5< /c10> * @Last Modified by:   Martin  6  * / 7namespace index; 8 include_once (' order.php '); 9 Ten // external Access class instantiation can be used  One  Use order\orderlist;  A $orderlist New orderlist;  - $orderlist-show_list ();  -  the // external access static variables and function direct Access  -  Use Order;  - Echo Order\str;  - Echo Order\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 directly 4 # include (' order.php '); 5 //Auto Load 6 Define (' Lib_dir ', __dir__. Directory_separator. ' Lib '. directory_separator); 7 spl_autoload_register (function ($class) {  8     $ Path$class . '. lib.php '; 9     include ($path); Ten });  One  spl_autoload_call (' Order ');  -  Use Order;  -  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:Use Get_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. (? )

http://www.bkjia.com/PHPjc/1059465.html www.bkjia.com true http://www.bkjia.com/PHPjc/1059465.html techarticle A preliminary study of PHP Namespaces and auto-loading: A preliminary study of PHP Namespaces reference to PHP Manual-language Reference: http://php.net/manual/zh/language.namespaces.php Overview: 1. Declares namespace ...

  • 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.