2016-12-14 33 views
-1

我有一個名爲config.php的文件,其中包含使用require_once的函數的不同文件。 當我嘗試使用存儲在項目根目錄中的.htaccess自動加入config.php時,我在每個包含函數的文件中都會收到一個重新聲明錯誤。PHP使用.htaccess時的Redeclare錯誤auto_prepend_file

有沒有辦法解決這個問題或包括config.php而沒有得到這個錯誤?

回答

0

好吧,我想出了一個解決方案,使用foreachglob。我不知道它是否有效,但它似乎工作。

// Include all files in X directory 
foreach (glob('X\*.php') as $file) 
{ 
    $script = basename($_SERVER['SCRIPT_NAME']); 
    $filename = basename($file); 
    if ($script != $filename) 
    { 
     include_once $file; 
    } 
} 

此代碼位於auto prepended文件中。