Code example
Copy code The 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 include_path, which is the address where the file is searched for when calling include in php.
The following 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. This class includes loadClass, loadFile, isReadable (whether the file is readable) and other functions
instantiation The process of Zend_Loader_Autoloader is the process of calling its constructor (here the singleton mode is used)
spl_autoload_register(array(__CLASS__, 'autoload')); in its constructor uses Zend_Loader_Autoloader:autoload as a class automatically Load 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 naturally uses Zend_Loader_Autoloader:autoload("USVN_Config_Ini")
The first step of this function will be to call getClassAutoloaders to obtain the AutoLoader of this class. GetClassAutoloaders adds the selection and judgment of namespaceAutoloader. Since we rarely use it, skip directly
The loader returned here is printed like this
Copy code The code is as follows:
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.
Zend_Loader_Autoloader:_autoload ("USVN_Config_Ini") will actually be called here
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? Looking at the initial definition of this class, it is actually array('Zend_Loader', 'loadClass');
What is naturally called below 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 at the Zend_Loader:loadClass method. The first step of this method is to check Exception, skip. 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), and then you have to The relative file path names introduced are read in separated by _ in the class name.
This is the end of reading the AutoLoad mechanism.
http://www.bkjia.com/PHPjc/326120.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326120.htmlTechArticleCode example Copy 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...