Php is automatically loaded. Php has two automatic loading functions: [php] _ autoload (); spl_autoload_register (); 1. _ autoload () can be used to load files into the program when classes are needed [php]? Phpfunction php has two automatic loading functions.
[Php]
_ Autoload ();
Spl_autoload_register ();
1. _ autoload ()
You can load files to programs when classes are needed.
[Php]
Function _ autoload ($ className ){
If (file_exists ($ className. '. php ')){
Include $ className. '. php'; // refined
} Else {
Echo $ className. '. php is not exists .';
Exit;
}
}
$ IndexController = new IndexController ();
During the running process of the program, php will check whether the $ className class has been loaded. if it is not loaded, it will execute _ autoload () and then load the $ className class. All the objects of the instantiated class, static variables and methods in the classes will check whether the class has been loaded and whether there is a defined _ autoload () function. if none of them exist, an error will be reported.
In systems with complex points, using _ autoload () to automatically load classes may be complicated.
2. spl_autoload_register ()
[Php]
Spl_autoload_register ();
$ Index = new Index ();
If the spl_autoload_register () function does not have any parameters, the void spl_autoload (string $ class_name [, string $ file_extensions]) function is automatically implemented by default. php and. ini are supported by default.
[Php]
Function load1 ($ className ){
// Include
}
Function load2 ($ className ){
// Include
}
Spl_autoload_register ('load1'); // register with spl_autoload_functions
Spl_autoload_register ('load2 ');
$ Index = new Index ();
The class will be loaded through load1 first. if load1 does not exist, it will be loaded through load2. if so, and so on.
There are many ways to implement automatic loading.
[Php]
Class autoloader {
Public static $ loader;
Public static function init ()
{
If (self: $ loader = NULL)
Self: $ loader = new self ();
Return self: $ loader;
}
Public function _ construct ()
{
Spl_autoload_register (array ($ this, 'model '));
Spl_autoload_register (array ($ this, 'helper '));
Spl_autoload_register (array ($ this, 'controller '));
Spl_autoload_register (array ($ this, 'Library '));
}
Public function library ($ class)
{
Set_include_path (get_include_path (). PATH_SEPARATOR. '/lib /');
Spl_autoload_extensions ('. library. php ');
Spl_autoload ($ class );
}
Public function controller ($ class)
{
$ Class = preg_replace ('/_ controller $/Ui', '', $ class );
Set_include_path (get_include_path (). PATH_SEPARATOR. '/controller /');
Spl_autoload_extensions ('. controller. php ');
Spl_autoload ($ class );
}
Public function model ($ class)
{
$ Class = preg_replace ('/_ model $/Ui', '', $ class );
Set_include_path (get_include_path (). PATH_SEPARATOR. '/model /');
Spl_autoload_extensions ('. model. php ');
Spl_autoload ($ class );
}
Public function helper ($ class)
{
$ Class = preg_replace ('/_ helper $/Ui', '', $ class );
Set_include_path (get_include_path (). PATH_SEPARATOR. '/helper /');
Spl_autoload_extensions ('. helper. php ');
Spl_autoload ($ class );
}
}
// Call
Autoloader: init ();
?>
You can also design and implement it based on your own needs.
Pipeline [php] _ autoload (); spl_autoload_register (); 1. _ autoload () can load the file into the program when classes are needed [php]? Php function...