我编写了一个包含目录中每个文件的PHP脚本。但我想知道是否有办法加载文件中的类,包括像自动加载器或其他什么?
<?php
define("include_dir", dirname(__FILE__) . '/includes/');
foreach (scandir(include_dir) as $filename)
{
if (is_file(include_dir . '/' . $filename))
{
//its a php file, lets do this!
if (substr($filename, -4) == '.php')
{
include include_dir . $filename;
}
}
}
?>
答案 0 :(得分:0)
试试这个 -
function __autoload($class_name) {
include $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
手册 -
http://php.net/manual/en/language.oop5.autoload.php