mrb*_*000 5 php config constants undefined
这是来自php手册:http://us.php.net/manual/en/language.constants.syntax.php
如果使用未定义的常量,PHP假定您指的是常量本身的名称,就像您将其称为字符串一样(CONSTANT vs"CONSTANT").发生这种情况时,将发出级别E_NOTICE的错误.
我真的不喜欢这种行为.如果我没有定义一个必需的常量,我宁愿脚本失败,所以我被迫定义它.如果它尝试使用未定义的常量,有没有办法强制PHP崩溃脚本?
例如.这两个脚本都做同样的事情.
<?php
define('DEBUG',1);
if (DEBUG) echo('Yo!');
?>
Run Code Online (Sandbox Code Playgroud)
和
<?php
if(DEBUG) echo('Yo!');
?>
Run Code Online (Sandbox Code Playgroud)
我宁愿第二个脚本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");
Run Code Online (Sandbox Code Playgroud)