包括类中的所有目录文件


include all directory files in class

我有一个这样的php文件

class Config {
   public static $__routes = array(
       "Home"                    => "index.php",
       "Support"                 => "support.php",
   );
}
Config::__routes += (include 'config/example1.php');
Config::__routes += (include 'config/example2.php');

我可以包含一个目录吗

例如:

include('include 'config/example1.php');
include('include 'config/example2.php');

将是这样的:

include('config/*');
您可以使用

glob尝试:

foreach (glob('config/*.php') as $file)
    include( $file );

此代码将包含给定文件夹中的所有.php文件。

<?php
if ($handle = opendir('/path/to/includes/folder')) {
    while (false !== ($entry = readdir($handle))) {
        $path_parts = pathinfo($entry);
    if ($path_parts['extension'] == '.php') include $entry;
    }
    closedir($handle);
}
?>

解释:OpenDIR 将打开给定的文件夹并返回文件夹的句柄。while 循环将遍历该文件夹中的所有文件。

pathinfo 将创建一个包含文件信息的数组,其中一个是文件的扩展名。

然后我们将找到的文件的扩展名与.php进行比较,如果它是php文件,我们将其包含在内。然后我们关闭打开文件夹的句柄。