Home  >  Article  >  Backend Development  >  AutoLoad mechanism in Zend Framework_PHP tutorial

AutoLoad mechanism in Zend Framework_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 10:58:39712browse

Code example

<ol class="dp-c">
<li class="alt"><span><span>set_include_path(USVN_LIB_DIR . PATH_SEPARATOR . get_include_path()); </span></span></li>
<li><span><span class="keyword">require_once</span><span> </span><span class="string">'Zend/Loader/Autoloader.php'</span><span>; </span></span></li>
<li class="alt"><span><span class="vars">$autoloader</span><span> = Zend_Loader_Autoloader::getInstance(); </span></span></li>
<li><span><span class="vars">$autoloader</span><span>->registerNamespace(</span><span class="string">"Zend_"</span><span>); </span></span></li>
<li class="alt"><span><span class="vars">$autoloader</span><span>->registerNamespace(</span><span class="string">"USVN_"</span><span>); </span></span></li>
<li><span><span class="vars">$autoloader</span><span>->registerNamespace(</span><span class="string">"menus_"</span><span>); </span></span></li>
<li class="alt"><span><span class="vars">$config</span><span> = </span><span class="keyword">new</span><span> USVN_Config_Ini(USVN_CONFIG_FILE, USVN_CONFIG_SECTION); </span></span></li>
</ol>

Process analysis

First, set the include_path, which is the address where the file is searched for when calling include in php

Below It is require_once 'Zend/Loader/Autoloader.php';

In the Zend/Loader/Autoloader.php file, Zend/Loader.php is read. This php defines the Zend_Loader class, which contains The process of instantiating Zend_Loader_Autoloader by loadClass, loadFile, isReadable (whether the file is readable) and other functions is the process of calling its constructor (here the singleton mode is used). The spl_autoload_register(array(__CLASS__, 'autoload') in its constructor ); Use Zend_Loader_Autoloader:autoload as the class autoloading function. I also did an operation to assign _internalAutoloader to its own _autoload

As for how to autoload here, I will check it out based on specific examples

Then I called Zend_Loader_Autoloader:registerNamespace("USVN_" ), what this function does is just mount a value with key USVN_ and value true on the internal attribute _namespaces of Zend_Loader_AutoLoader.

When you see this function, you will understand that the code can also be written as

$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 is a natural choice It is Zend_Loader_Autoloader:autoload("USVN_Config_Ini"). The first step of this function is to call getClassAutoloaders to obtain the AutoLoader of this class. GetClassAutoloaders adds the selection and judgment of namespaceAutoloader. Since we rarely use it, skip the returned loader and print it out like this

Array ( [0] => Zend_Loader_Autoloader Object ( [_autoloaders:protected ] => Array ( ) [_defaultAutoloader:protected] => Array ( [0] => Zend_Loader [1] => loadClass ) [_fallbackAutoloader:protected] => [_internalAutoloader:protected] => Array * RECURSION* [_namespaces:protected] => Array ( [Zend_] => 1 [ZendX_] => 1 [USVN_] => 1 [menus_] => 1 ) [_namespaceAutoloaders:protected] => Array ( ) [_suppressNotFoundWarnings:protected] => [_zfPath:protected] => ) [1] => _autoload )

is actually the _internalAutoloader set earlier.

Here Zend_Loader_Autoloader:_autoload ("USVN_Config_Ini") will actually be called

Okay, now you see the Zend_Loader_Autoloader:_autoload function

$callback = $this-> getDefaultAutoloader();

The default Autoloader will be obtained here. What is the default Autoloader? Look at the initial definition of this class, it is actually array('Zend_Loader', 'loadClass');

below Naturally, the call is call_user_func($callback, $class); that is, Zend_Loader:loadClass("USVN_Config_Ini")

First of all, Zend_Loader has been required in AutoLoader.php

Secondly, let’s take a look Zend_Loader: loadClass method, the first step of this method is to check for exceptions and skip it. The second step is to separate the classes and piece them together into $file, such as USVN/Config/Ini.php. Next, directly call self::loadFile($file, null, true);

Then check self: :loadFile, first _securityCheck checks whether there are illegal characters in the class name. If not, the $file is included. Of course $file here is a relative path and needs to be spliced ​​with include_path. Do you remember where include_path is set? Set it up at the beginning of the program! Okay, here is the USVN_Config_Ini class read in.

You should understand after seeing this. If you define a class yourself and register a Namespace, such as USVN, then you should create a folder with the same name under include_path (case must be distinguished). Then the relative file path name you want to import is read separated by _ in the class name.

This is the end of reading the AutoLoad mechanism.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445667.htmlTechArticleCode example set_include_path(USVN_LIB_DIR.PATH_SEPARATOR.get_include_path()); require_once 'Zend/Loader/Autoloader.php'; $autoloader =Zend_Loader_Autoloader::getInstance(); $auto...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn