Php class registration and automatic loading of _ autoload. _ Autoload () is a function agreed in the PHP execution environment, rather than a class method. if a class is not loaded to the current file before use, the _ autoload () function is automatically called to add _ autoload () as a function agreed in the PHP execution environment, rather than a class method, if a class is not loaded to the current file before use, the _ autoload () function is automatically called to load the class. Generally, the loading rules of these classes are agreed, for example, if these classes are included in the file named after the class name, this method can implement on-demand loading of classes to avoid loading unnecessary classes before the script is executed, thus reducing the resource occupation and submission performance.
Note: __errors in autoload () cannot be caught by try-catch.
| The code is as follows: |
|
|
Function _ autoload ($ class_name ){
Require_once (PATH. '/calsses/'. $ class_name. '. php ');
}
$ Obj1 = new mycalss1 (); |
Register the _ autoload () auto-called function:
The spl code library is automatically enabled after PHP5.0 by default.
Spl_autoload_register ([callback]); // you can use this function to register a callback function instead of writing the loaded code in _ autoload.
If you use the class method as the callback function, you need to input an array:
| The code is as follows: |
|
|
Spl_autoload_register (array ('class _ name' | $ obj, 'method _ name '));
For example:
Spl_autoload_register (array ($ this, 'autoloadclass ')); |
Spl_autoload_register (array ('yibase', 'autoload'); // implement the automatic loading class of the YII Framework. the YiiBase class implements an autoload method. Spl_autoload_register () can register multiple loading functions. before successfully loading class files, all registered loading functions will be tried one by one. This is useful when different classes use different logic to import class files.
Spl_autoload_unregister (); // cancel a registered function. the parameter is the same as spl_autoload_register.
Spl_autoload_functions (); // returns all registered _ autoload () functions in array form
Spl_autoload (class_name [, file_extentions]); // the default implementation of the _ autoload () function. If the function name is not input when spl_autoload_register () is called, this function is used by default. the execution rule of this function is to convert the class name to lowercase as the file name, input file_extentions (multiple extensions are separated by commas. the default value is. inc and. php. search in include paths set in ini.
Spl_autoload_call (class_name); // manually call all registered _ autoload () functions to actively load class files
Spl_autoload_extentions ([file_extentions]); // register or return the file extension that can be used in spl_autoload (). The extension can be in the form of. a. B, for example:
| The code is as follows: |
|
|
Spl_autoload_extentions (". class. php ");
Spl_autoload_register (); // use spl_autoload () to automatically load class files
// In this case, spl_autoload ('myclassname'); will try to load the file "myclassName. class. php ". |
Instance
1. Place the classes to be registered in an array.
| The code is as follows: |
|
|
Final class Utils {
Private function _ construct (){
}
Public static function getClasses ($ pre_path = '/'){
$ Classes = array (
'Dbconfig' => $ pre_path. 'dbconfig/DBConfig. php ',
'User' => $ pre_path. 'model/User. php ',
'Dao '=> $ pre_path. 'Dao/Dao. php ',
'Userdao '=> $ pre_path. 'Dao/UserDao. php ',
'Usermapper' => $ pre_path. 'ing ing/UserMapper. php ',
);
Return $ classes;
}
}
?> |
2. register an array
Note: the class paths in step 1 are relative to init. php, not to Utils, because we use the automatic loading function spl_autoload_register in init. php to request the ire class.
| The code is as follows: |
|
|
Require_once '/Utils. php ';
Final class Init {
/**
* System config.
*/
Public function init (){
// Error reporting-all errors for development (ensure you have
// Display_errors = On in your php. ini file)
Error_reporting (E_ALL | E_STRICT );
Mb_internal_encoding ('utf-8 ');
// Registe classes
Spl_autoload_register (array ($ this, 'loadclass '));
}
/**
* Class loader.
*/
Public function loadClass ($ name ){
$ Classes = Utils: getClasses ();
If (! Array_key_exists ($ name, $ classes )){
Die ('class "'. $ name.'" not found .');
}
Require_once $ classes [$ name];
}
}
$ Init = new Init ();
$ Init-> init ();
?>
|
3. in this example, require init. php in test. php is used.
| The code is as follows: |
|
|
Require_once 'init. php ';
$ Dao = new UserDao ();
$ Result = $ dao-> findByName ('zcl ');
?> |
Http://www.bkjia.com/PHPjc/631580.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631580.htmlTechArticle__autoload () is the method of a function agreed in the PHP execution environment, rather than a class, if a class is not loaded to the current file before use, it will automatically call _ autoload () function to add...