我有這三個文件一起工作:嵌套的`include()`指令 - 如何包含來自多個目錄的文件(包含另一個文件)?
document_root/include/config.php
document_root/include/database.php
document_root/index.php
和文件內容的相關部分是象下面這樣:
的config.php
// ...
$MyVar = 100;
// ...
database.php中
// ...
require('config.php');
// Aiming to use the definitions in "config.php" here
// ...
的index.php
// ...
require('include/database.php');
// Using the code in "database.php" here
// ...
的問題是,config.php
以某種方式不包括在內,並且還沒有錯誤消息中給出(在E_ALL模式)。在index.php
中運行代碼時,我無法從database.php
文件訪問config.php
文件中的定義。
在我的PHP.ini
文件中include_path
設置爲C:\...\document_root\include
目錄。
PHP版本是5.3.0。
我觀察到的是,如果我改變database.php
的require()
指令爲
require('include/config.php');
代碼的無故障運行,一切都剛剛好。但是這種解決方案在實踐中是不可能的,因爲我打算從多個位置包含config.php
文件。
這個問題的原因是什麼?
我該如何解決它?
任何幫助將不勝感激。
您的回答非常好,非常感謝。 但是我想知道爲什麼在包含文件時必須指定路徑,即使已經在'PHP.ini'指令'include_path'中指定了它的路徑? 爲什麼這個通用設置在這裏不適用? – hkBattousai 2011-05-31 12:07:07
其實我自己有點困惑。 [include的文檔](http://www.php.net/manual/en/function.include.php)表示「... ** include()**將最終檢入調用腳本自己的目錄.. 「,這意味着你首先不應該有問題。我確定引用的部分在上次檢查時不在那裏。 – Oswald 2011-05-31 17:53:30