由於某些原因,我在代碼下面發現了這個通知,我的應用程序工作正常,並且不會以任何方式影響代碼的其餘部分。但我無法圍繞這一個通知包裹我的頭。我在代碼中看不到任何錯誤。另外,我在其他地方使用了_ROOT全局常量,它沒有給我任何關於它未定義的通知。有趣的是,if (defined('_ROOT'))
評估爲真實,因爲它顯然確實是確定的。爲什麼當使用定義的常量時,__autoload會給我e_notice警告?
代碼:
<?php
session_start();
//define('_DEBUG', 'YES');
define('_ROOT', dirname(__FILE__), true);
require_once _ROOT.'/config/config.php'; //no notice
function __autoload($class_name) {
if (defined('_DEBUG')) { echo '__autoload called<br>'; }
if (defined('_ROOT')) { echo 'root exists'._ROOT.'<br>'; } //doesn't give me a notice
if (file_exists(_ROOT.'/app/core/'.$class_name.'.php')) { //gives me a notice
require_once _ROOT.'/app/core/'.$class_name.'.php'; //doesn't give me a notice
}
}
$app = new Application();
echo $app->run();
注意:Notice: Use of undefined constant _ROOT - assumed '_ROOT' in path\to\index.php on line 13
此代碼在我的本地環境中正常使用PHP 5.3.5。你可能已經檢查過了,但是你能檢查這條線是否確實引發了注意,而不是其他的?我經常發現自己正在檢查一個不同的文件/行:)另外,嘗試使用變量,例如'$ file = _ROOT。'/ app/core /'.$ class_name。'。php''和'if(file_exists($ file ))'。 –
因爲這不是第18行,你能發佈所有18行嗎?你確定你正在查找正確的文件嗎?也重新鍵入_ROOT在第18行 - 也許其中一個是cirylic或其他的東西,看起來像O但不是相同的字符 –
我不知道爲什麼這可能是。然而,你可以把'if(file_exists ...){require_once ..}'代碼放入if(defined('_ ROOT'))'conditional?對於這樣一個奇怪的錯誤,這不是一個理想的解決方案 - 但我根本沒有看到爲什麼通知應該被打印。 – Pete171