Introduction to Zend's AutoLoad mechanism

  • 2020-05-26 07:54:47
  • OfStack

Code sample

 
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

First, include_path is set. include_path is the address the file looks for when include is called in php
Below is require_once 'Zend/Loader/Autoloader php';

In Zend/Loader/Autoloader php file, read the Zend/Loader php, this defines Zend_Loader php this class, this class contains loadClass, loadFile, isReadable (if the file can be read), and other functions
The process of instantiating Zend_Loader_Autoloader is the process of calling its constructor (in this case, using the singleton pattern)

In its constructor spl_autoload_register(array(s) 45en__, 'autoload')); Consider Zend_Loader_Autoloader:autoload as the class auto-loading function.
We also did an operation to assign _internalAutoloader to its own _autoload

As for how autoload is in it, I will check it later according to specific examples
Zend_Loader_Autoloader:registerNamespace("USVN_") is then called, and all this function does is mount a value of key for USVN_ and value for true on the internal property _namespaces of Zend_Loader_AutoLoader.

When you look at this function, you can actually write the code as well

$autoloader- > registerNamespace("Zend_")- > registerNamespace("USVN_")
or
$autoloader- > registerNamespace(array("Zend_","USVN_"))

Ok, now it's time to call the USVN_Config_Ini class
Zend_Loader_Autoloader:autoload("USVN_Config_Ini")
The first step of this function is to call getClassAutoloaders to get the AutoLoader of this class. The selection and judgment of namespaceAutoloader has been added to getClassAutoloaders. Since we seldom use namespaceAutoloader, we will skip it directly

The loader returned here prints 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 ) 


So this is just _internalAutoloader.

This will actually call Zend_Loader_Autoloader:_autoload ("USVN_Config_Ini")
Ok, so now you see Zend_Loader_Autoloader:_autoload function

$callback = $this- > getDefaultAutoloader();
So this is going to get the default Autoloader, what is the default Autoloader? If you look at the initial definition of this class, it's actually array('Zend_Loader', 'loadClass');
The next natural call is call_user_func($callback, $class); Namely Zend_Loader: loadClass (" USVN_Config_Ini ")

First of all, Zend_Loader is already require in AutoLoader.php
Next, let's look at the Zend_Loader:loadClass method, whose first step is to check for exceptions and skip. Step 2 is the class space, made $file, such as USVN/Config/Ini php, here are direct call self: : loadFile ($file, null true);

Next check out self::loadFile,
First _securityCheck looks for any illegal characters in the class name, no, just include the $file. The $file here is of course a relative path, so we need to splicing include_path, remember where include_path was set? It was set at the beginning of program 1! Okay, so we're reading in USVN_Config_Ini.
You should understand that if you have defined a class and registered Namespace, such as USVN, you should create a folder with the same name under include_path (case sensitive), and then the relative file path name you want to introduce is read separately from the _ of the class name.

That's the end of the AutoLoad mechanism.

Related articles: