AutoLoad mechanism _php Tutorial in the Zend framework

Source: Internet
Author: User
Tags autoload autoloader vars zend framework
code example

 
 
  1. Set_include_path (Usvn_lib_dir. Path_separator. Get_include_path ());
  2. require_once ' zend/loader/autoloader.php ';
  3. $autoloader = Zend_loader_autoloader::getinstance ();
  4. $autoloader ->registernamespace ("Zend_");
  5. $autoloader ->registernamespace ("Usvn_");
  6. $autoloader ->registernamespace ("Menus_");
  7. $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 ( The process of instantiating a zend_loader_autoloader, such as a file is readable, is to call its constructor (here is the use of Singleton mode) and the Spl_autoload_register in its constructor (array (__class__, ' AutoLoad ')); 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 class naturally goes to 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 added to the namespaceautoloader of the choice and judgment, because we rarely use, directly skip the return here Loader print out is like this

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 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/445667.html www.bkjia.com true http://www.bkjia.com/PHPjc/445667.html techarticle code example Set_include_path (Usvn_lib_dir. Path_separator.get_include_path ()); Require_once ' zend/loader/autoloader.php '; $autoloader =zend_loader_autoloader::getinstance (); $auto ...

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