PHP框架 如果包含一般文件较少的情况会用手动包含要使用的类文件
当要包含大量类文件的时候,这样就会显得麻烦,就可以使用自动包含类。
类文件:test.php
class Test
{
public function __construct()
{
echo __CLASS__.__FUNCTION__;
}
}
1.手动包含:
require_once('test.php');
$test = new Test();
2.使用__autoload()自动包含:
// 这样实例化一个类的时候,将会自动包含同名的类文件
// 需要重载__autoload方法,自定义包含类文件的路径
function __autoload($classname)
{
$class_file = strtolower($classname).".php";
if (file_exists($class_file)){
require_once($class_file);
}
}
$test = new Test();


