Note: __errors in autoload () cannot be caught by try-catch.
The code is as follows: |
Copy code |
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: |
Copy code |
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: |
Copy code |
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: |
Copy code |
<? Php 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: |
Copy code |
<? Php 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: |
Copy code |
<? Php Require_once 'init. Php '; $ Dao = new UserDao (); $ Result = $ dao-> findByName ('zcl '); ?> |