PHP 中是否有一个配置选项可以防止未定义的常量被解释为字符串?
这是来自 php 手册: http://us.php.net /manual/en/language.constants.syntax.php
如果您使用未定义的常量,PHP 会假定您指的是常量本身的名称,就像您将其作为字符串调用一样(CONSTANT 与“CONSTANT”)。发生这种情况时,将发出 E_NOTICE 级别的错误。
我真的不喜欢这种行为。如果我未能定义所需的常量,我宁愿脚本失败,以便我被迫定义它。如果 PHP 尝试使用未定义的常量,有什么方法可以强制 PHP 使脚本崩溃吗?
例如。这两个脚本都做同样的事情。
<?php
define('DEBUG',1);
if (DEBUG) echo('Yo!');
?>
我
<?php
if(DEBUG) echo('Yo!');
?>
宁愿第二个脚本 DIE 并声明它尝试使用未定义的常量 DEBUG。
This is from the php manual: 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.
I really don't like this behavior. If I have failed to define a required constant, I would rather the script fail so that I am forced define it. Is there any way to force PHP to crash the script if it tries to use an undefined constant?
For example. Both of these scripts do the same thing.
<?php
define('DEBUG',1);
if (DEBUG) echo('Yo!');
?>
and
<?php
if(DEBUG) echo('Yo!');
?>
I would rather the second script DIE and declare that it tried to use an undefined constant DEBUG.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以做这样的事情(丑陋):
伪代码:
set_error_handler()
http://us.php.net/manual/en/class。错误异常.php#95415
You could do something (ugly) like this:
pseudo code:
set_error_handler()
http://us.php.net/manual/en/class.errorexception.php#95415
我认为没有办法更改抛出的错误类型,但您可以使用 error_reporting 以便您在开发时看到这些错误:
I don't think there's a way to change the type of error thrown, but you can change the error reporting to
E_ALL
using error_reporting so that you see these errors while developing: