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.
- 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.
- 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.
- 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:
- An object that implements the Zend_Loader_Autoloader_Interface Interface
$autoloader = Zend_Loader_Autoloader::getInstance();$myAutoloaderClass = new my_Autoloader();$autoloader->pushAutoloader($myAutoloaderClass, myNamespace);
- A function
$autoloader = Zend_Loader_Autoloader::getInstance();function myAutoloaderFun(){ //TODO };$autoloader->pushAutoloader('myAutoloaderFun', myNamespace);
- Call the method of an object or class in the callback method (return the call_user_func function)
$ 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.