About spl_autoload_register() and __autoload()
Look at the usage of both:
//__autoload usage function __autoload($classname) { $filename = "./class/".$classname.".class.php"; if (is_file($filename)) { include $filename; } }
The benefits of using spl_autoload_register() are indescribable: (1) Automatically loading objects is more convenient, and many frameworks do this:
class ClassAutoloader { public function __construct() { spl_autoload_register(array($this, 'loader')); } private function loader($className) { echo 'Trying to load ', $className, ' via ', __METHOD__, "() n"; include $className . '.php'; } }
(2) You need to know that the __autoload() function can only exist once. Of course, spl_autoload_register() can register multiple functions
function a () { include 'a.php'; } function b () { include 'b.php'; } spl_autoload_register('a'); spl_autoload_register('b');
(3) SPL functions are rich and provide more functions, such as spl_autoload_unregister() to unregister registered functions, spl_autoload_functions() to return all registered functions, etc.