Home > Backend Development > PHP Tutorial > Instructions for using PHP's autoload automatic loading mechanism_PHP tutorial

Instructions for using PHP's autoload automatic loading mechanism_PHP tutorial

WBOY
Release: 2016-07-21 15:32:52
Original
950 people have browsed it

During PHP development, if you want to introduce a class from the outside, you usually use the include and require methods to include the file that defines the class. However, this may cause a large number of include or If the require method is called accidentally, an error will occur, making the code difficult to maintain.

Since PHP5, the __autoload interceptor method has been introduced, which can automatically include references to class files. Usually we will write like this:

Copy code 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, giving PHP the last chance to load the required class before it fails with an error. . Therefore, when the above code is executed, the PHP engine actually automatically executes the __autoload method for us and includes the file User.class.php.

Exceptions thrown in the __autoload function cannot be caught by the catch statement block and result in a fatal error.

If you use PHP's CLI interactive mode, the autoloading mechanism will not be executed.

When you want to use PEAR style naming rules, for example, you need to import the User/Register.php file, you can also do it like this:
Copy code The code is as follows:

//Load me
function __autoload($className) {
$file = str_replace('_', DIRECTORY_SEPARATOR, $className);
include_once $ file . 'php';
}
$userRegister = new User_Register();


Although this method is convenient, if multiple classes are introduced in a large application When using libraries, some inexplicable problems may occur due to the autoload mechanisms of different class libraries. After the introduction of the SPL standard library in PHP5, we have a new solution, the spl_autoload_register() function.

The function of this function is to register the function into the __autoload function stack of SPL and remove the system default __autoload() function. Once the spl_autoload_register() function is called, when an undefined class is called, the system will call 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. Not User_Register.class.php:
Copy code 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 safer initialization call method, as follows:
Copy the code The code is as follows:

//System default __autoload function
function __autoload($className) {
include_once $className . '.class.php' ;
}
//__autoload function for SPL loading
function autoload($className) {
$file = str_replace('_', DIRECTORY_SEPARATOR, $className);
include_once $file . '.php';
}
//Accidentally loaded the wrong function name, and at the same time canceled the default __autoload mechanism...囧
spl_autoload_register('_autoload', false);
//Fault tolerance mechanism
if(false === spl_autoload_functions()) {
if(function_exists('__autoload')) {
spl_autoload_register('__autoload', false);
}
}

Strange trick: In a Unix/Linux environment, if you have multiple smaller classes, for the convenience of management, when they are all written in a php file, It can be quickly distributed into multiple copies with different class names by making soft links with the ln -s command, and then loaded through the autoload mechanism.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322768.htmlTechArticleDuring the PHP development process, if you want to introduce a class from the outside, you usually use the include and require methods to The file that defines this class is included, but this may cause...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template