PHP Automatic loading class file discussion, Spl_autoload_register automatic loading principle

Source: Internet
Author: User
Tags spl

The Spl_autoload_register function is an important way to implement automatic loading of undefined class functions, so called automatic loading means that our new class must first include or require class files if there is no include or require , you will get an error. So we have to write a lot of include or require files on the head of the file, very troublesome

To make it normal to have a new class without an include or require class, there is the concept of automatic loading, meaning that the new class does not have to include the class file beforehand, so that our file header does not contain many include (require). Actually, it's a package!

Using the Spl_autoload_register function can achieve the above function, let us look at the implementation of the principle. The parameters of this function are as follows:

Autoload_function
This is a function "method" name, which can be a string or an array (called by the class method to use). The function (method) is to take the class file that needs new to include the Include (Requeire), so that the new will not find the file. In fact, encapsulates the entire project's include and require functions.

Throw
This parameter sets whether Spl_autoload_register () throws an exception when Autoload_function fails to register successfully.

Prepend
If True,spl_autoload_register () adds the function to the top of the queue instead of the tail of the queue.

This autoload_function method is executed when we new a class and the class file is not included:
Let's take a look at the error example:

// when we go directly to the new one that doesn't contain the class file, we get an error . $objDemo New Autoloadclass (); /* *fatal error:uncaught error:class ' autoloadclass ' not found in E:\work\demo\spl\autoloadDemo.php on line 3error:cla SS ' Autoloadclass ' not found in E:\work\demo\spl\autoloadDemo.php on line 3* /

Using the Spl_autoload_register method

//file autoloadclass.php, require new fileclassautoloadclass{ Public function__construct () {Echo' You've included me. '; }}//Files autoloaddemo.php fileSpl_autoload_register (' Myautoload ',true,true);functionMyautoload ($className){    Echo"All of the included files are given to me!" \ r \ n "; $classFileName= "./{$className}.php "; Echo"I come to contain!" {$classFileName}\r\n "; include"./{$className}.php ";}$objDemo=NewAutoloadclass ();/** Output: All the included file work is handed to me! I'm here to contain!./autoloadclass.php you've included me.*/

As we can see from the above example, when new does not contain a class, it executes the function that spl_autoload_register the first parameter function name, and this function has a parameter that requires the class name of new. The function is to include this class in (the class name and file name are consistent), so that the automatic loading function is implemented. The principle is this, not very complicated.
In addition we can change to an anonymous function to implement:

Spl_autoload_register (function($className){    Echo"All of the included files are given to me!" \ r \ n "; $classFileName= "./{$className}.php "; Echo"I come to contain!" {$classFileName}\r\n "; include"./{$className}.php ";},true,true);$objDemo=NewAutoloadclass ();/** Output: All the included file work is handed to me! I'm here to contain!./autoloadclass.php you've included me.*/

PHP Automatic loading class file discussion, Spl_autoload_register automatic loading principle

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.