Zend AutoLoad mechanism introduction _ PHP Tutorial

Source: Internet
Author: User
Tags autoload autoloader
Zend AutoLoad mechanism introduction. Sample code copy code: set_include_path (USVN_LIB_DIR.PATH_SEPARATOR.get_include_path (); require_onceZendLoaderAutoloader.php; $ autoloaderZend_Loader_Au Sample 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 step is to set include_path, which is the address of the file when php calls include.
The following is require_once 'zend/Loader/Autoloader. php ';

In Zend/Loader/Autoloader. the PHP file reads Zend/Loader. php. This php defines the Zend_Loader class, which contains functions such as loadClass, loadFile, and isReadable (whether the file is readable ).
The process of instantiating Zend_Loader_Autoloader is to call its constructor (the Singleton mode is used here ).

In its constructor, spl_autoload_register (array (_ CLASS __, 'autoload'); uses Zend_Loader_Autoloader: autoload as the CLASS to automatically load the function.
An operation is also performed to assign _ internalAutoloader to its own _ autoload.

As for how to use autoload, we will refer to the specific example.
Next we call Zend_Loader_Autoloader: registerNamespace ("USVN _"), this function only mounts a value with the key USVN _ and value true on the internal attribute _ namespaces of Zend_Loader_AutoLoader.

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

$ Autoloader-> registerNamespace ("Zend _")-> registerNamespace ("USVN _")
Or
$ Autoloader-> registerNamespace (array ("Zend _", "USVN _"))

Now we have called the USVN_Config_Ini class.
This class naturally follows 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 determination of namespaceAutoloader. because we seldom use it, skip it directly.

The returned loader is printed like this.

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)



It is actually the _ internalAutoloader set above.

Here, Zend_Loader_Autoloader: _ autoload ("USVN_Config_Ini") is actually called ")
Now we can see the Zend_Loader_Autoloader: _ autoload function.

$ Callback = $ this-> getDefaultAutoloader ();
The default Autoloader will be obtained here. what is the default Autoloader? The initial definition of this class is actually array ('zend _ loader', 'loadclass ');
The following calls call_user_func ($ callback, $ class); that is, Zend_Loader: loadClass ("USVN_Config_Ini ")

First, Zend_Loader has been require in AutoLoader. php.
Next, let's take a 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 classes and piece them together into $ file, such as USVN/Config/Ini. php. the following calls self: loadFile ($ file, null, true) directly );

Next, check self: loadFile,
First, check whether the class name contains invalid characters. if not, include this $ file. The $ file here is of course a relative path and needs to be spliced with include_path. do you remember where include_path was set? Set it at the beginning of the program! Now, we will read the USVN_Config_Ini class.
If you have defined a class and registered a Namespace, such as USVN, then 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 by class name.

Here, the AutoLoad mechanism is complete.

Example code: set_include_path (USVN_LIB_DIR. PATH_SEPARATOR. get_include_path (); require_once 'zend/Loader/Autoloader. php'; $ autoloader = Zend_Loader_Au...

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.