PHP:如何包括目录中所有子目录中的所有文件

问题描述

我想包括许多子目录中的许多PHP文件。但是如果必须为每个子目录使用include,将非常烦人。

例如:

include "./subdir1/*.PHP";
include "./subdir2/*.PHP";
include "./subdir3/*.PHP";

那么,有没有一种方法可以仅使用一个include语句来包含目录中所有子目录中的所有文件

解决方法

使用php glob函数。下面的示例代码

foreach (glob(document_root.'/class/*.php') as $filename) {
    require_once($filename);
}