在PHP中是否有一个配置选项来防止未定义的常量被解释为字符串?

这是来自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

上一篇:项目升级,为了热更新使用lua。


下一篇:php – 更改Woocommerce中显示的变体数量