请告诉我这个是否正确。在我的错误处理程序中,我需要能够检测到何时使用了@ error-control操作符来抑制错误,因为一些外部库(遗憾的是)经常使用它。脚本的执行应该继续,就像您不使用自定义错误处理程序时一样。
当使用at-sign时,PHP会将error_reporting临时设置为0。因此,在脚本的开头,我们将error_reporting设置为除零以外的任何值--我们现在可以做一些漂亮的IF/ELSE魔术。为了避免在前端显示任何错误,我们还将display_errors设置为0,这将覆盖error_reporting (但我们仍然可以使用它的值作为魔术)。
<?php
ini_set('display_errors',0);
error_reporting(E_ALL);
function error_handler($errno, $errstr, $errfile, $errline)
{
if (error_reporting()===0) return;
else die();
}
set_error_handler('error_handler');
//This issues an error, but the handler will return and execution continues.
//Remove the at-sign and the script will die()
@file();
echo 'Execution continued, hooray.';
?>所以..。这里没有捕获物吗?除了外部库覆盖了我的错误处理..(对此有什么建议吗?)
发布于 2009-08-16 10:04:23
考虑到您的脚本所做的工作,以及@ operator manual page上的一些用户注释,您所做的似乎是可以的。
例如,taras says:
我对@符号的实际作用感到困惑,经过几次实验得出了以下结论:
的错误级别调用它
set_error_handler手册页面似乎证实了这一点:
特别要注意的是,如果导致错误的语句前面加上了@
-control运算符,则此值将为0。
这里也有一些有用的用户注释;例如,this one (请参阅代码的开头)
不过,如果您想要的是“禁用”@运算符的效果(不确定我是否正确理解了问题;这可能会对您有所帮助),为了能够在开发环境中获得错误消息,您可以安装scream扩展(pecl、manual)。
假设你以正确的方式配置它,在你的php.ini中设置它(当然是在安装/加载扩展之后):
scream.enabled = 1这个扩展只会禁用@运算符。
下面是一个例子(引用manual):
<?php
// Make sure errors will be shown
ini_set('display_errors', true);
error_reporting(E_ALL);
// Disable scream - this is the default and produce an error
ini_set('scream.enabled', false);
echo "Opening http://example.com/not-existing-file\n";
@fopen('http://example.com/not-existing-file', 'r');
// Now enable scream and try again
ini_set('scream.enabled', true);
echo "Opening http://example.com/not-existing-file\n";
@fopen('http://example.com/another-not-existing-file', 'r');
?>这将输出:
Opening http://example.com/not-existing-file
Opening http://example.com/not-existing-file
Warning: fopen(http://example.com/another-not-existing-file): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in example.php on line 14我不确定我是否会在生产服务器上使用这个扩展(在那里我从来不想显示错误),但它在开发机器上非常有用,当使用旧代码时,在广泛使用@ operator的应用程序/库上...
https://stackoverflow.com/questions/1283919
复制相似问题