Introduction to the autoload mechanism of Zend _php tutorial

Source: Internet
Author: User
Tags autoload autoloader
code example

Copy CodeThe code is as follows:
Set_include_path (Usvn_lib_dir. Path_separator. Get_include_path ());
Require_once ' zend/loader/autoloader.php ';
$autoloader = Zend_loader_autoloader::getinstance ();
$autoloader->registernamespace ("Zend_");
$autoloader->registernamespace ("Usvn_");
$autoloader->registernamespace ("Menus_");
$config = new Usvn_config_ini (Usvn_config_file, usvn_config_section);


Process Analysis

The first is to set the Include_path,include_path is the address that the file looks for when the include is called in PHP
The following is require_once ' zend/loader/autoloader.php ';

In the zend/loader/autoloader.php file, read the zend/loader.php, this PHP defines the Zend_loader class, this class contains Loadclass,loadfile, IsReadable ( file is readable) functions such as
The process of instantiating a zend_loader_autoloader is to call its constructor (this is the case with singleton mode)

The Spl_autoload_register in its constructor (array (__class__, ' autoload ')), and Zend_loader_autoloader:autoload as the class auto-load function.
Also made an operation to assign _internalautoloader to its own _autoload

As for how the AutoLoad in this case will be viewed according to specific examples
The next call to Zend_loader_autoloader:registernamespace ("Usvn_"), this function is only in the internal properties of Zend_loader_autoloader _ namespaces mount a value with a key of Usvn_ and value true.

See this function to understand that the code can also be written

$autoloader->registernamespace ("Zend_")->registernamespace ("Usvn_")
Or
$autoloader->registernamespace (Array ("Zend_", "Usvn_"))

Okay, now it's time to call the Usvn_config_ini class.
This kind of nature walks is zend_loader_autoloader:autoload ("Usvn_config_ini")
The first step of this function is to call getclassautoloaders to get the autoloader of this class. Getclassautoloaders inside adds to the choice and judgment of namespaceautoloader, as we seldom use, skip directly over

Here's the return of the loader printed out like this
Copy CodeThe code is as follows:
Array ([0] = Zend_loader_autoloader Object ([_autoloaders:protected] = = Array () [_defaultautoloader:protected] = = Array ([0] = zend_loader [1] = loadclass) [_fallbackautoloader:protected] = [_internalautoloader:prote CTED] = array *recursion* [_namespaces:protected] = Array ([zend_] = 1 [Zendx_] = 1 [usvn_] + 1 [Menu S_] + 1) [_namespaceautoloaders:protected] + Array () [_suppressnotfoundwarnings:protected] = [_zfpath:prot Ected] = [1] = _autoload)


is actually the _internalautoloader set up earlier.

This will actually call Zend_loader_autoloader:_autoload ("Usvn_config_ini")
Okay, now you see the Zend_loader_autoloader:_autoload function.

$callback = $this->getdefaultautoloader ();
This will get the default autoloader, what is the default autoloader? Look at the initial definition of this class, which is actually an array (' Zend_loader ', ' loadclass ');
The following natural call is Call_user_func ($callback, $class); Zend_loader:loadclass ("Usvn_config_ini")

First of all, Zend_loader has been require in autoloader.php.
Next we look at the Zend_loader:loadclass method, the first step of this method is to check for exceptions and skip. The second step is to separate the classes into $file, such as usvn/config/ini.php, directly calling Self::loadfile ($file, NULL, TRUE);

Next look at Self::loadfile,
First _securitycheck See if there are any illegal characters in the class name, no, include this $file. Here $file of course is relative path, need splicing on include_path, remember Include_path is where set? It was set up at the beginning of the program! Well, here's the Usvn_config_ini class that reads it in.
See here you should understand, if you define a class, and registered namespace, such as USVN, then you should create a folder with the same name under include_path (case must be differentiated), and then you want to introduce the relative file path name is the name of the class _ Do the separation read into.

Here the autoload mechanism is read.

http://www.bkjia.com/PHPjc/326120.html www.bkjia.com true http://www.bkjia.com/PHPjc/326120.html techarticle The Code example copies the code code as follows: Set_include_path (Usvn_lib_dir. Path_separator. Get_include_path ()); Require_once ' zend/loader/autoloader.php '; $autoloader = Zend_loader_au ...

  • Related Article

    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.