这是来自php手册:http://us.php.net/manual/en/language.constants.syntax.php
If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a string (CONSTANT vs “CONSTANT”). An error of level E_NOTICE will be issued when this happens.
我真的不喜欢这种行为.如果我没有定义一个必需的常量,我宁愿脚本失败,所以我*定义它.如果它尝试使用未定义的常量,有没有办法强制PHP崩溃脚本?
例如.这两个脚本都做同样的事情.
<?php
define('DEBUG',1);
if (DEBUG) echo('Yo!');
?>
和
<?php
if(DEBUG) echo('Yo!');
?>
我宁愿第二个脚本DIE并声明它尝试使用未定义的常量DEBUG.
最佳答案:
你可以这样做(丑陋):
伪代码:
/**
* A Notice becomes an Error :)
*/
function myErrorHandler($errno, $errstr, $errfile, $errline) {
if ($errno == E_NOTICE) { // = 8
if (substr($errstr ... )) { // contains something which looks like a constant notice...
trigger_error('A constant was not defined!', E_USER_ERROR);
}
}
}
set_error_handler("myErrorHandler");
>退房set_error_handler()
>检查这个伟大的评论:
http://us.php.net/manual/en/class.errorexception.php#95415