How does Zend_Loader_Autoloader work?

Source: Internet
Author: User

The principle of automatic loading is that Zend_Loader_Autoloader is used in the Zend_Application instance to call spl_autoload_register (array (_ CLASS __, 'autoload ')); in this way, if the class cannot be found, the autoload method of this object will be called for processing.

A namespace can have multiple loaders. When a custom loader is found through iteration, the rest will not be used, generally, we only register a custom loader for a namespace or directly use the default loader.

You can register a namespace in two ways:

  • Only the pre-region of the space is registered, and no loader is specified. The registration method is registerNamespace ('space name ').
  • Register the pre-space handler and specify the loader. The registration method is pushAutoloader (loader, 'space name') or unshiftAutoloader (loader, 'space name ') the difference between the two methods is that push puts the loader after the loader queue with the specified space name, before unshift.

When a class cannot be found and loaded is required, the class name is handed over to Zend_Loader_Autoloader: autoload () for processing. The process is as follows:

  • Use the prefix of the registered namespace to compare with this type of name to find the loader specified by the namespace.
    1. Use the namespace registration method. The second kind of name prefix is compared with the name prefix to find the loader specified by the namespace.
    2. Use the namespace registration method to compare the prefix of the first type of name and the prefix of this type of name. If so, use the Zend loader.
    3. If there is no namespace before the Delimiter is equal to this class, And the FallbackAutoloader flag is set, the Zend loader is also used. Otherwise, no loader is returned, and this class cannot be loaded.
  • If the loader implements the Zend_Loader_Autoloader_Interface interface class, it will pass the class name to its autoload Method for loading. If the loader is a function, it will load the class name as the parameter of this function. If the loader is an array, use the call_user_func callback function for loading.

A custom loader can take the following forms:

  1. An object that implements the Zend_Loader_Autoloader_Interface Interface
  2. $autoloader = Zend_Loader_Autoloader::getInstance();$myAutoloaderClass = new my_Autoloader();$autoloader->pushAutoloader($myAutoloaderClass, myNamespace);
  3. A function
  4. $autoloader = Zend_Loader_Autoloader::getInstance();function myAutoloaderFun(){ //TODO };$autoloader->pushAutoloader('myAutoloaderFun', myNamespace);
  5. Call the method of an object or class in the callback method (return the call_user_func function)
  6. $ Autoloader = Zend_Loader_Autoloader: getInstance (); $ autoloader-> pushAutoloader (array ('class name', 'method'), myNamespace );

The Zend loader object method is Zend_Loader_Autoloader: _ autoload. This method uses call_user_func to call the final loading method. For example, the default value is array ('zend _ load', 'loadclass') to load the current object.

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.