PHP autoload automatic loading mechanism instructions _ PHP Tutorial

Source: Internet
Author: User
Tags spl
PHP autoload automatic loading mechanism instructions. 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, however, this may cause the include and require methods to be used to include the file defining this class if you want to introduce a class from the outside during PHP development, however, this may cause a large number of include or require method calls in the new script of the referenced file. if you miss it for a moment, an error will occur, making the code difficult to maintain.

Since PHP5, The _ autoload interceptor method is introduced to automatically include and reference class files. we usually write this as follows:

The code is as follows:


Function _ autoload ($ className ){
Include_once $ className. '. class. php ';
}

$ User = new User ();


When the PHP engine tries to instantiate an operation of an unknown class, it will call the _ autoload () method, and there is a last chance to load the required class before the PHP error fails. Therefore, when the above code is executed, the PHP engine automatically executes the _ autoload method for us to include the File User. class. php.

The exception thrown in the _ autoload function cannot be caught by the catch statement block and cause a fatal error.

If you use the CLI interaction mode of PHP, the automatic loading mechanism will not be executed.

If you want to use PEAR-style naming rules, such as introducing the User/Register. php file, you can do the following:

The code is as follows:


// Load me
Function _ autoload ($ className ){
$ File = str_replace ('_', DIRECTORY_SEPARATOR, $ className );
Include_once $ file. 'php ';
}
$ UserRegister = new User_Register ();



This method is convenient, but when multiple class libraries are introduced in a large application, some inexplicable problems may occur due to the autoload mechanism of different class libraries. After PHP5 introduced the SPL standard library, we added a new solution, the spl_autoload_register () function.

This function registers the function to the _ autoload function stack of SPL and removes the default _ autoload () function. 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, the following example calls User/Register. php instead of User_Register.class.php:

The code is as follows:


// Do not load me
Function _ autoload ($ className ){
Include_once $ className. '. class. php ';
}
// Load me
Function autoload ($ className ){
$ File = str_replace ('/', DIRECTORY_SEPARATOR, $ className );
Include_once $ file. '. php ';
}
// Start loading
Spl_autoload_register ('autoload ');
$ UserRegister = new User_Register ();



When using spl_autoload_register (), we can also consider using a more secure initialization call method. refer to the following:

The code is as follows:


// System default _ autoload function
Function _ autoload ($ className ){
Include_once $ className. '. class. php ';
}
// The _ autoload function that can be loaded by SPL
Function autoload ($ className ){
$ File = str_replace ('_', DIRECTORY_SEPARATOR, $ className );
Include_once $ file. '. php ';
}
// Accidentally loaded the wrong function name, and canceled the default _ autoload mechanism ...... Bytes
Spl_autoload_register ('_ autoload', false );
// Fault tolerance mechanism
If (false === spl_autoload_functions ()){
If (function_exists ('_ autoload ')){
Spl_autoload_register ('_ autoload', false );
}
}


Amazing: in Unix/Linux environments, if you have multiple small-sized classes, you can write them in a php file for ease of management, you can use the ln-s command as a soft link to quickly distribute copies of multiple different class names, and then load them using the autoload mechanism.

...

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.