簡體   English   中英

在類中包括所有目錄文件

[英]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 try:

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文件,則將其包括在內。 然后我們關閉打開的文件夾的句柄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM