您需要使用 PHP 的自动加载功能,我在下面为您提供了一个示例。
function __autoload($class_name) {
$class_name = strtolower($class_name); // you may need to omit this or rename your files
$file = "class.{$class_name}.php";
$directory = "/path/to/your/class/directory/";
if($full_path = recursive_file_exists($file, $directory)) {
require($full_path);
} else {
// check if it exists with an s on the end
// a nice fallback to cover forgetfulness
$file = "class.{$class_name}s.php";
if($full_path = recursive_file_exists($file, $directory)) {
require($full_path);
} else {
die("The file class.{$class_name}.php could not be found in the location ".$directory);
}
希望对您有所帮助。