[PHP] _ autoload () magic method and spl_autoload_register, splregisterautoload

Source: Internet
Author: User
Tags autoload autoloader php error

[PHP] _ autoload () magic method and spl_autoload_register, splregisterautoload

Reference link:

1. Details about the differences between spl_autoload_register and autoload

2. php.net automatic loading class

 

Many developers create a PHP source file for each class definition when writing object-oriented applications. A major headache is that you have to write a long list of contained files (one file for each class) at the beginning of each script ).

In PHP 5, this is no longer needed. You can define a _ autoload () function, which is automatically called when trying to use a class that has not been defined. By calling this function, the script engine has the last chance to load the required class before a PHP error fails.

 

Simple Introduction example:

Create a new class file MyClass. class. php:

<?phpclass MyClass{    public function __construct()    {        echo "This is MyClass's construct";    }}

 

Create a PHP file with any name in the same directory:

<? Phpfunction _ autoload ($ cls) {var_dump ($ cls); require '. /'. $ cls. ". class. php ";}$ o1 = new MyClass (); // here _ autoload will not be executed, because this class has already been require. The _ autoload function $ o2 = new MyClass () is called only when the class cannot be found in the current file ();

Here, the require content is placed in _ autoload (). If this function is not defined, a Fatal error (Fatal) is reported, prompting that the MyClass class cannot be found.

When this magic method is defined in the new object and the class definition cannot be found in the current Code (the above two conditions are met), __autoload () the function is passed in a class name parameter and executed in the code segment.

Therefore, var_dump ("MyClass"); and require './MyClass. class. php' are executed in the function ';

 

The result is as follows:

string(7) "MyClass" This is MyClass's constructThis is MyClass's construct

The content of var_dump and echo of constructor are output here. It can be proved that when require comes in, the new MyClass class will not call the _ autoload () function again.

 

Autoload has a disadvantage: it cannot be defined repeatedly (the characteristics of PHP language), which makes it inconvenient for a team to develop and is prone to conflicts.

So after PHP 5.1.2, there is an autoload registration function spl_autoload_register. The following are some descriptions of this function:

Register the function to the SPL _ autoload function queue. If the functions in the queue are not activated, activate them.

If multiple autoload functions are required, spl_autoload_register () meets these requirements. It actually creates a queue for the autoload function and runs it one by one in the defined order. In contrast, _ autoload () can be defined only once.

 

After the namespace is introduced in PHP 5.3, the parameters of this function have multiple forms:

Spl_autoload_register ('My _ autoloader '); // my_autoloader is a function name spl_autoload_register (array ('loader', 'loadclass'); // represents the static method in the class, loader: loadClass // or, you can use an anonymous function spl_autoload_register (function ($ class) {include 'classes/'since PHP 5.3.0 /'. $ class. '. class. php ';}); spl_autoload_register (_ NAMESPACE __. '\ Foo: test'); // from PHP 5.3.0, The namespace format can be used.

 

This function only needs to be called before the new object.

 

Here is an example (test the above four forms ):

<? Php // namespace Atl; // an error is returned when this sentence is added! Function my_autoloader ($ cls) {echo "<br/> $ cls-my_autoloader <br/>"; require_once '. /MyClass1.class. php ';} class Loader {public static function loadClass ($ cls) {echo "<br/> $ cls-Loader: loadClass <br/>"; require_once '. /MyClass2.class. php ';} public static function nspClass ($ cls) {echo "<br/> $ cls-Loader: nspClass <br/>"; require_once '. /MyClass3.class. php ';}} spl_autoload_register ('My _ autoloader'); $ o1 = new MyClass1 (); spl_autoload_register (array ('loader ', 'loadclass ')); $ o2 = new MyClass2 (); // version check if (version_compare (PHP_VERSION, '5. 3.0 ',' <') die ("the following two tests require PHP 5.3 and later"); spl_autoload_register (_ NAMESPACE __. '\ Loader: nspclass'); $ o3 = new MyClass3 (); spl_autoload_register (function ($ cls) {echo "<br/> $ cls-anonymous function <br/>"; require_once '. /MyClass4.class. php ';}); $ o4 = new MyClass4 ();

 

The output result is as follows (note that the search order is the same as the registration order ):

MyClass1 - my_autoloader This is MyClass1's constructMyClass2 - my_autoloader MyClass2 - Loader::loadClass This is MyClass2's constructMyClass3 - my_autoloader MyClass3 - Loader::loadClass MyClass3 - Loader::nspClass This is MyClass3's constructMyClass4 - my_autoloader MyClass4 - Loader::loadClass MyClass4 - Loader::nspClass MyClass4 - anonymous function This is MyClass4's construct

 

Related Article

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.