There are no parameters in the spl_autoload_register() function, and the void spl_autoload (string $class_name [,string $file_extensions]) function will be automatically implemented by default. It supports .php and .ini by default
The class will be loaded through load1 first. If there is no class in load1, it will be loaded through load2, and so on.
There are many ways to implement automatic loading. Here is an example
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();
?>
也可以根据自己的需要来设计实现
http://www.bkjia.com/PHPjc/477831.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477831.htmlTechArticlephp中有两种自动加载机制函数 [php] __autoload(); spl_autoload_register(); 1. __autoload() 可以将需要使用类的时候把文件加载到程序中 [php] ?php function...