Php automatic loading mechanism

Source: Internet
Author: User
Tags spl
1. the methods for automatic loading in php are manually loaded using require, include, require_once and include_once. Use _ autoload for automatic loading use spl autoload for automatic loading of manual loading: when there are few files to be loaded, we can... "/> <scripttype =" text/jav

I. php automatic loading method

 

Use require, include, require_once, and include_once for manual loading.

Use _ autoload for automatic loading
Use 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.


[Php]
Require_once 'A. php ';
Require_once 'B. php ';
Require_once 'C. php ';

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.


[Php]
Echo 'I'm in. php under test.
';

Echo 'I'm in. php under test.
'; Then create a loader. php file in the test Directory. the content is as follows.

[Php]
// 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. 

// 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 (); // after the command is executed, the in. php file under test is displayed! 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.


[Php]
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 ();

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.


Functions automatically loaded by spl
Spl_autoload is the default implementation of _ autoload (). it will go to include_path to find $ class_name (. php/. inc) Spl_autoload for automatic loading:
Create in. php in the test Directory. the content is as follows:


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

Class in {
Public function index (){
Echo 'I'm in. php' under test ';
}
}
?> Create loader. php in the test Directory. the content is as follows:
[Html]
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 ();

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.
[Php]
Function AutoLoad ($ class ){
If ($ class = 'in '){
Require_once ("/var/www/test/in. php ");
}
}
Spl_autoload_register ('autoload ');
$ A = new in ();
$ A-> index ();

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 is used to register multiple custom autoload functions.
First, create the mod folder under the test directory and create inmod. mod. php as follows:
[Php]
Class inmod
{
Function _ construct ()
{
Echo 'I am the in under the mods ';
}
}

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:
[Php]
Class inlib
{
Function _ construct ()
{
Echo 'I am in libs ';
}
}

Class inlib
{
Function _ construct ()
{
Echo 'I am in libs ';
}
} Finally, create loader. php in the test Directory as follows:
[Php]
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. 

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 (); // output I am in
New inlib (); // output I 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.