PHP class automatic loading mechanism _ PHP Tutorial

Source: Internet
Author: User
Tags spl
PHP class automatic loading mechanism. During PHP development, if you want to introduce a class from the outside, the include and require methods are usually used to include the file defining this class. During small-scale development, if you want to introduce a class from the outside during PHP development, the include and require methods are usually used to include the file defining this class. This is not a big problem during small-scale development. However, in large development projects, this will produce a large number of require or include method calls, which will not reduce the efficiency, but also make the code difficult to maintain. besides, require_once is very costly.

Before PHP5, if PHP frameworks need to implement automatic class loading, they generally implement a traversal directory according to certain conventions and automatically load all classes or functions that comply with the agreed rules. Of course, PHP5's previous object-oriented support was not very good, and classes were not frequently used. After PHP5, when a PHP class is loaded, the Zend Engine automatically calls the _ autoload function if the class file is not included or the class name is incorrect. You need to implement the _ autoload function by yourself. After PHP5.1.2, you can use the spl_autoload_register function to customize automatic loading of processing functions. If this function is not called, the spl_autoload function customized by SPL is used by default.

1. example of _ autoload:

Function _ autoload ($ class_name ){
Echo '_ autload class:', $ class_name ,'
';
}

New Demo ();
The above code will output :__ autload class: Demo at the end.
The following error message is displayed: Fatal error: Class 'demo' not found.

We generally use the _ autoload automatic loading class as follows:


Function _ autoload ($ class_name ){
Require_once ($ class_name. "class. php ");
}
$ Memo = new Demo ();
We can see that _ autoload should do at least three things. The first thing is to determine the class file name based on the class name, the second thing is to determine the disk path of the class files (in our example, the simplest case is that the class is in the same folder as the PHP program file that calls them ), the third thing is to load the class from the disk file to the system. The third step is the simplest. you only need to use include/require. To implement the first step and the second step, you must specify the ing method between the class name and the disk file during development. Only in this way can we find the corresponding disk file based on the class name.

Therefore, when a large number of class files need to be included, we only need to determine the corresponding rules, and then in the _ autoload () function, you can map the class name to the actual disk file to achieve the lazy loading effect. Here we can also see that the most important thing in the implementation of the _ autoload () function is the implementation of the ing rule between the class name and the actual disk file.

But now the problem arises. if many other class libraries need to be used in the implementation of a system, these class libraries may be developed by different development engineers, the ing rules for class names and actual disk files are different. In this case, to automatically load the class library file, all the ing rules must be implemented in the _ autoload () function. Therefore, the _ autoload () function may be very complex, it cannot even be implemented. In the end, the _ autoload () function may be very bloated. even if it can be implemented, it will bring a huge negative impact to future maintenance and system efficiency. In this case, introduce the SPL standard library in PHP5, a new solution, namely the spl_autoload_register () function.

2. spl_autoload_register () function

This function registers the function to the _ autoload function stack of SPL and removes the default _ autoload () function. The following example shows that:


Function _ autoload ($ class_name ){
Echo '_ autload class:', $ class_name ,'
';
}
Function classLoader ($ class_name ){
Echo 'SPL load class: ', $ class_name ,'
';
}
Spl_autoload_register ('classloader ');
New Test (); // Result: SPL load class: Test
Syntax: bool spl_autoload_register ([callback $ autoload_function]) accepts two parameters: one is the function added to the auto-load stack, and the other is the flag that the loader cannot find whether to throw an exception when the class is found. The first parameter is optional and is directed to the spl_autoload () function by default. This function will automatically find the class name with lower case in the path. php extension or. ini extension, or any file registered with another extension in the spl_autoload_extensions () function.


Class CalssLoader
{
Public static function loader ($ classname)
{
$ Class_file = strtolower ($ classname). ". php ";
If (file_exists ($ class_file )){
Require_once ($ class_file );
}
}
}
// The method is static.
Spl_autoload_register ('calssloader: loader ');
$ Test = new Test ();
Once the spl_autoload_register () function is called, the system calls all functions registered to the spl_autoload_register () function in sequence, instead of automatically calling the _ autoload () function. To avoid this situation, you need to use a safer method for calling the spl_autoload_register () function:

If (false === spl_autoload_functions ()){
If (function_exists ('_ autoload ')){
Spl_autoload_registe ('_ autoload', false );
}
}
The spl_autoload_functions () function returns an array of the registered functions. if the SPL automatically loads the stack and is not initialized yet, it returns a boolean value of false. Then, check whether a function named _ autoload () exists. If yes, you can register it as the first function in the auto load stack to retain its function. Then, you can continue to register the auto-load function.

You can also call the spl_autoload_register () function to register a callback function, instead of providing a String name for the function. If you provide an array such as array ('class', 'method'), you can use the method of an object.

Next, by calling the spl_autoload_call ('classname') function, you can manually call the loader without trying to use that class. This function can be combined with the class_exists ('classname', false) function to try to load a class, in addition, if no auto-loader can find the class, it fails.

F (spl_autoload_call ('classname') & class_exists ('classname', false )){

} Else {
}
The SPL automatic loading function is provided by the spl_autoload (), spl_autoload_register (), spl_autoload_functions (), spl_autoload_extensions (), and spl_autoload_call () functions.
(Network content summary)



From Program life, guisu column

Bytes. This is a small-scale development...

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.