First, __autoload
This is an auto-load function, which is triggered when we instantiate an undefined class in PHP5. Look at the following example:
printit.class.php //File <?php class PrintIt { function doprint () { echo ' Hello World ';}}? > index.php //File <?phpfunction __autoload ($class) {$file = $class. '. class.php '; if (Is_file ($file)) { require_once ($file);} } $obj = new PrintIt (); $obj->doprint ();? >
Second, Spl_autoload_register ()
1. It tells PHP to execute the specified class when it encounters a class that is not defined;
<?phpfunction Loadprint ($class) {$file = $class. '. class.php '; if (Is_file ($file)) { require_once ($file);} } Spl_autoload_register (' Loadprint '); $obj = new PrintIt (); $obj->doprint ();? >
2, Spl_autoload_register () call static method
<?phpclass Test {public static function Loadprint ($class) { $file = $class. '. class.php '; if (Is_file ($file)) { require_once ($file);}} } Spl_autoload_register ( Array (' Test ', ' Loadprint ') );//Another notation: Spl_autoload_register ( "test:: Loadprint " ); $obj = new PrintIt (); $obj->doprint ();? >
PHP automatic loading of two functions __autoload and __sql_autoload_register