In-depth analysis of php automatic loading mechanism

Source: Internet
Author: User
This article provides a detailed analysis of the php automatic loading mechanism. For more information, see I. php automatic loading method
1. use require, include, require_once, and include_once for manual loading.
2. use _ autoload for automatic loading
3. use the spl autoload for automatic loading
Implementation of manual loading:

When there are few files to load, we can use the first one. This is easy and no problem.

The code is as follows:


Require_once 'A. php ';
Require_once 'B. php ';
Require_once 'C. php ';


But is it okay to load many files? What should we do when we need to write ten, 20 require_once or more?

In this case, we can use the _ autoload method to simplify our code.

_ Autoload loading implementation:
Create an in. php file in the test Directory. the content is as follows.

The code is as follows:


Echo 'I'm in. php under test.
';


Create a loader. php file in the test Directory. the content is as follows.

The code is as follows:


// The _ autoload method needs to be reloaded to customize the path containing class files
Function _ autoload ($ classname)
{
$ Class_file = strtolower ($ classname). ". php ";
If (file_exists ($ class_file )){
Require_once ($ class_file );
}
}
@ $ Test = new in (); // output after executionI am in. php under test.


No problem. it's successful! We can also create other files for loading. but what should we do when many files need to be separated from directories?

In this case, we need to modify loader. php to use ING to find the file to be loaded.

The code is as follows:


Function _ autoload ($ class_name ){
$ Map = array (
'Index' => './include/index. php ',
'In' => './in. php'
);

If (file_exists ($ map [$ class_name]) & isset ($ map [$ class_name]) {
Require_once $ map [$ class_name];
}
}
New index ();


The advantage of this method is that the class name and file path are only maintained by a ing. Therefore, when the file structure changes, you do not need to modify the class name, you only need to modify the corresponding items in the ING.

However, _ autoload can only be used once in a project. when your project references another project, your project contains _ autoload, another project also has a _ autoload, so the two _ autoload conflicts. the solution is to change _ autoload into one, which is undoubtedly very cumbersome and has a single application scenario.

Implementation of spl autoload loading:
Spl autoload functions use one autoload call stack. you can use spl_autoload_register to register multiple custom autoload functions, which are widely used.

• Create in. php in the test Directory. the content is as follows:

The code is as follows:


Class in {
Public function index (){
Echo 'I'm in. php' under test ';
}
}
?>


Create loader. php in the test Directory. the content is as follows:

The code is as follows:


Set_include_path ("/var/www/test/"); // put the path in include
Spl_autoload ("in"); // search for/var/www/test/in. php
$ In = new in ();
$ In-> index ();


• Spl_autoload_register registers the function to the SPL _ autoload function stack and modifies loader. php.

The code is as follows:


Function AutoLoad ($ class ){
If ($ class = 'in '){
Require_once ("/var/www/test/in. php ");
}
}
Spl_autoload_register ('autoload ');
$ A = new in ();
$ A-> index ();


• Spl_autoload_register registers multiple custom autoload function applications
First, create the mod folder under the test directory and create inmod. mod. php as follows:

The code is as follows:


Class inmod
{
Function _ construct ()
{
Echo 'I am the in under the mods ';
}
}


Create the libs folder in the test Directory and create inlib. lib. php as follows:

The code is as follows:


Class inlib
{
Function _ construct ()
{
Echo 'I am in libs ';
}
}


Finally, create loader. php in the test Directory as follows:

The code is as follows:


Class Loader {
/**
* Automatically load classes
* @ Param $ class name
*/
Public static function MOD ($ class ){
If ($ class ){
Set_include_path ("/var/www/test/MoD /");
Spl_autoload_extensions (". mod. php ");
Spl_autoload (strtolower ($ class ));
}
}
Public static function libs ($ class ){
If ($ class ){
Set_include_path ("/var/www/test/libs /");
Spl_autoload_extensions (". lib. php ");
Spl_autoload (strtolower ($ class ));
}
}
}
Spl_autoload_register (array ('loader ', 'Mods '));
Spl_autoload_register (array ('loader ', 'libs '));
New inmod (); // outputI am an in under the MoD
New inlib ();//OutputI am in libs.

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.