Zend_Loader Class Structure and Function Analysis

Source: Internet
Author: User
Tags autoload autoloader

Zend_Loader Class Structure and Function Analysis

 

Zend_Loader is used to dynamically load classes and files. All the methods of this class are static. The class structure is as follows:

 

 

 About Zend_Loader and require_once ():

Zend_Loader is mainly used to load files that are identified by variables mapped to dynamic requests (for example, the names of the files to be loaded come from user input or parameters of a method ), A program is hosted to load the corresponding file.

If you only load a specified file or class (that is, a specified file or class, such as/lib/test. PHP), the use of the traditional PHP function require_once () or include_once () is more appropriate, and the use of zend_loader is redundant.

 

This class has four core functions:

1. Load file zend_loader: LoadFile ($ filename)

This method is used to load PHP files. In fact, it encapsulates include () or include_once () and adds the file name security check.

Public static function LoadFile ($ filename, $ dirs = NULL, $ once = false)

Parameter description:

The $ filename parameter specifies the file to be loaded. $ Filename must be the complete file name containing the extension (for example, ". php"), but no path is required.

The file name is checked for security. The file name can only contain letters, numbers, connectors (-), underscores (_), and periods. $ Dirs parameter is not limited.

$ Dirs can be a directory string or a directory array. The default value is null. This method searches for the specified file in the specified directory in the Order provided by the Directory parameters, and try to load the first matching file.

If the file is not found in the specified $ dirs, or the parameter is not specifically set, this method will try to load the file from the plude_path in PHP. In fact, this method sets the $ dirs

Path_separator connects to the front end of include_path to append the string, and then include or include_once to load the file.

$ Once: the optional parameter is used to specify whether the file is loaded at a time. That is, if it is set to true, use include_once to load the file. Otherwise, use include to load the file by default.

2. Load class Zend_Loader: loadClass ($ class, $ dirs)

Load a Class from the php file. The file name must be in the format of $ class. php. load the file and check whether the Class exists.

Publicstaticfunction loadClass ($ class, $ dirs = null)

Parameter description:

$ Class name. The file name of the corresponding class must be in the format of $ class. php.

$ Dirs: the directory to be searched. It can be an array composed of directory strings or directory strings. The default value is null;

This method searches for the corresponding class files in the Order provided by $ dirs. The first matching file will be loaded. If this type of file does not exist in the specified dirs,

Or, if $ dirs is not specified, search for it from the export de_path in the PHP environment. Throw exception if the file does not exist or the class does not exist after the file is loaded.

Function:

1) First, determine whether the specified class or interface exists. If yes, return directly. Otherwise, determine whether the specified $ dirs is valid, that is, whether it is null or a string or an array. Otherwise, throw is abnormal.

2) then the class name string "$ class" will be converted to a relative path by replacing the underscore (_) with the corresponding directory separator Based on the operating system OS.

For example, in the following example, find and load the Container_Tree class in the specified directory, and the class name will be mapped to this path level structure: "Container/Tree. php ",

In windows, "Container \ Tree. php" is used ".

3) Since PHP introduced the namespace concept from PHP5.3 and started from zendFramework1.10, classes can be loaded from the custom PHP namespace.

Loading classes from PHP namespace by ZF follows the following three rules:

A) when loading from the file system, The namespace separator ("\") is converted to the directory SeparatorDIRECTORY_SEPARATOR("\" Or "/").

B) The underline "_" in the class file name will be converted into a directory separator.DIRECTORY_SEPARATOR("\" Or "/"). The underscore "_" has no special meaning in the namespace of PHP.

C) when loading from a file system, the complete namespace and class are suffixed with ". php.

For example, the following ing relationship:

\ Doctrine \ Common \ IsolatedClassLoader =>/Path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader. php 

\ Namespace \ package \ Class_Name =>/Path/to/project/lib/vendor/namespace/package/Class/Name. php 

\ Namespace \ package_name \ Class_Name =>/Path/to/project/lib/vendor/namespace/package_name/Class/Name. php 

Therefore, in the previous step, you need to separate the namespace and the class name with the namespace separator, and then convert them to the relative path according to the preceding three rules.

4) Call self: loadFile ($ file, $ dirs, true) to use the specified $ dir and the relative path converted from class $ class as the directory to complete file loading.

5) Finally, determine whether the class exists; otherwise, throw is abnormal.

3. Determine whether the specified file exists and is readable. Zend_Loader: isReadable ($ pathname)

Static Method Zend_Loader: isReadable ($ pathname) determines whether a file exists and is readable. If it is readable, TRUE is returned. Otherwise, FALSE is returned.

 

If (Zend_Loader: isReadable ($ filename )){
// Do something with $ filename
}

 

The $ filename parameter specifies the file name to be checked, including the path information. This method is encapsulated by the PHP function is_readable.

Is_readable () does not automatically search for files under include_path, while Zend: isReadable () does.

4. register and use the Autoloader function.

Zend_Loader'sAutoloadAndRegisterAutoloadThe method is removed from version 1.8 and will be removed from ZF2.0.

1 ),Zend_Loader: autoload ($ class)The method only references and encapsulates Zend_Loader: loadClass ($ class ).

Use this method as the auto-loader method. Instead, use the Zend_Loader_Autoloader: autoload ($ class) method as the default auto-loader method.

2 ),Zend_Loader: registerAutoload($ Class = 'zend _ Loader ', $ enabled = true ),

This method first sets Zend_Loader_Autoloader as a backup loader. See the Code:

Require_once 'zend/Loader/Autoloader. php ';
$ Autoloader = Zend_Loader_Autoloader: getInstance ();
$ Autoloader-> setFallbackAutoloader (true );

The significance of setting the backup loader is that when loading a class, if there is no corresponding namespace loader for this class and there is no more than a namespace loader, Zend _ Loader_Autoloader will be used

The internal automatic loader _ internalAutoloader = array ($ this, '_ autoload'); while the _ autoload method actually uses the Zend_Loader: loadClass method.

Then, if the loader to be registered is not Zend_Loader, it is registered as an automatic loader and placed in the autoloaders stack and namespaceAutoloaders stack.

 

 

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.