有什么方法可以显示或抛出PHP警告? [英] Is there any way to show, or throw, a PHP warning?
问题描述
我在数据库类中有一个select()
方法,该方法具有可选的布尔参数$sum
.此参数用于说明该方法是否应该也使用COUNT(*)
.
I have a select()
method in a database class, that has an optional boolean argument $sum
. This argument is used to say if the method should use COUNT(*)
or not too.
如果未设置属性(例如,当我使用$sum == false
调用select()
时)尝试访问$class->sum
,我想显示警告,就像那些普通的PHP错误一样.
I would like to show a warning, like those normal PHP errors, if I try to access $class->sum
if the attribute is not set (i.e. when I call select()
with $sum == false
.
有什么办法可以显示这样的警告,还是我应该回显一个错误并感到高兴?
Is there any way to show a warning like this, or I should just echo an error and be happy?
推荐答案
如果要生成警告,则应编写
If you want to generate a warning, you should write
trigger_error($yourErrorMessage, E_USER_WARNING);
trigger_error()
具有$error_type
参数,用于设置错误级别(Notice
,Warning
或Fatal error
).常量分别是:
trigger_error()
has the $error_type
parameter for setting the error level (Notice
, Warning
or Fatal error
). The constants are, respectively:
E_USER_NOTICE // Notice (default)
E_USER_WARNING // Warning
E_USER_ERROR // Fatal Error
请注意,Fatal error
停止了后续PHP代码的执行,而Notice
和Warning
使其继续执行.
Note that Fatal error
stops the execution of subsequent PHP code, while Notice
and Warning
let it continue.
从PHP 5.5开始,您还应考虑最后语句.
From PHP 5.5, you should also consider the Finally statement.
这篇关于有什么方法可以显示或抛出PHP警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!