PHP class Auto-loading mechanism _php tutorial

Source: Internet
Author: User
Tags spl php framework
In the process of PHP development, if you want to introduce a class from the outside, you will typically use the include and require methods to include the file that defines the class. This is not a big problem when it comes to small-scale development. However, in large-scale development projects, doing so generates a large number of require or include method calls, which makes the code difficult to maintain without compromising efficiency, and the cost of require_once is great.

Before PHP5, each PHP framework, if it was to implement the automatic loading of classes, would typically implement a class or function that iterates through the directory and automatically loads all files that conform to the Convention rules by some Convention. Of course, PHP5 's previous support for object-oriented is not very good, and the use of classes is not very frequent now. After PHP5, the Zend engine calls the __autoload function automatically when the PHP class is loaded, if the file is not included in the class, or if the class name is wrong. This function requires the user to implement the __autoload function themselves. After the PHP5.1.2 version, you can use the Spl_autoload_register function to customize the auto-load handler function. When this function is not called, the SPL custom spl_autoload function is used by default.

1. __autoload Example:

function __autoload ($class _name) {
Echo ' __autload class: ', $class _name, '
';
}

New Demo ();
The above code will be output at the end: __autload Class:demo.
And after this error shows: Fatal error:class ' Demo ' not found

We generally use the _autoload auto-load class as follows:


function __autoload ($class _name) {
Require_once ($class _name. "class.php");
}
$memo = new Demo ();
We can see that _autoload do at least three things, the first thing is to determine the class file name based on the class name, and the second thing is to determine the disk path where the class files are located (in our case the simplest case, the class is in the same folder as the PHP program file that called them), The third thing is to load the class from the disk file into the system. The third step is the simplest, only need to use Include/require. To implement the first step, the second step of the function, you must contract the class name and disk file mapping method at development time, so that we can find its corresponding disk file according to the class name.

Therefore, when there are a large number of class files to include, we just need to determine the corresponding rules, and then in the __autoload () function, the class name and the actual disk file corresponding to the effect of lazy loading can be achieved. From here we can also see that the most important implementation of the __autoload () function is the implementation of the class name and the actual disk file mapping rules.

But now the problem is, if in a system implementation, if you need to use a lot of other class libraries, these libraries may be developed by different development engineers, the class name and the actual disk file mapping rules are different. If you want to implement the automatic loading of the class library file, you must implement all the mapping rules in the __autoload () function, so the __autoload () function can be very complex or even impossible to implement. In the end, the __autoload () function can be bloated, and even if it can be achieved, it will have a significant negative impact on future maintenance and system efficiency. In this case, the PHP5 introduces the SPL standard library, a new solution, the Spl_autoload_register () function.

2. Spl_autoload_register () function

The function is to register the function in the __autoload function stack of SPL and remove the system default __autoload () function. In the following example, you can see:


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 when the class throws an exception. The first parameter is optional and, by default, points to the spl_autoload () function, which automatically finds the path with a lowercase class name and a. php extension or an. ini extension, or any registered to Spl_autoload_extensions () The file for other extensions in the function.


Class Calssloader
{
public static function loader ($classname)
{
$class _file = Strtolower ($classname). ". PHP ";
if (file_exists ($class _file)) {
Require_once ($class _file);
}
}
}
Method is a static method
Spl_autoload_register (' Calssloader::loader ');
$test = new test ();
Once the Spl_autoload_register () function is called, all functions registered to the Spl_autoload_register () function are called sequentially, rather than automatically called by the __autoload () function when the class is called undefined. If this is to be avoided, a more secure initialization invocation method for the Spl_autoload_register () function is required:

if (false = = = Spl_autoload_functions ()) {
if (function_exists (' __autoload ')) {
Spl_autoload_registe (' __autoload ', false);
}
}
The Spl_autoload_functions () function returns an array of registered functions and returns a Boolean value of False if the SPL auto-load stack has not been initialized. Then, check to see if there is a function named __autoload () that, if present, can register it as the first function in the auto-load stack, preserving its functionality. After that, 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 '), it makes it possible to use an object's method.

Next, by calling the Spl_autoload_call (' className ') function, you can call the loader manually instead of trying to use that class. This function can be combined with the function class_exists (' ClassName ', false) to try to load a class and fail if none of the autoloader can find that class.

F (Spl_autoload_call (' className ') && class_exists (' ClassName ', false)) {

} else {
}
The SPL auto-load function is provided by Spl_autoload (), Spl_autoload_register (), Spl_autoload_functions (), Spl_autoload_extensions () and Spl_ Provided by the Autoload_call () function.
(Web content Summary)



Excerpt from the program Life, guisu column

http://www.bkjia.com/PHPjc/478291.html www.bkjia.com true http://www.bkjia.com/PHPjc/478291.html techarticle In the process of PHP development, if you want to introduce a class from the outside, you will typically use the include and require methods to include the file that defines the class. 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.