1、function __autoload($className);
// 检测加载的类是否存在
function __autoload($className)
{
$path = $className . '.php';
if(file_exists($path))
{
include_once($className . '.php');
}else{
exit('no file');
}
}
2、spl_autoload_register()
spl_autoload_register()会自动先调用spl_autoload()在路径中查找具有小写文件名的”.php”程序.默认查找的扩展名还有”.ini”,还可以用spl_autoload_extenstions()注册扩展名.。在找不到的清况下,还可以通过自己定义函数查找 。
(1)
function load($class){
include_once("C:\\test\\" . $class . '.php');
}
spl_autoload_register('load');
(2)
class Loader
{
/**
* 自动加载类
* @param $class 类名
*/
public static function autoload($class)
{
include_once( "c:\\test\\" . $class . '.php');
}
}
/**
* sql自动加载
*/
spl_autoload_register(array('Loader', 'autoload'));