今天在写一个类时,要将PHP的默认错误处理函数接管,使用自定义的错误处理函数,在函数中,将错误封装成ErrorException后抛出,发现抛出的异常跟踪信息中,函数的参数错位了,下面是这个问题的示例代码:
<?php
class ErrorHandle
{
function __construct()
{
set_error_handler(array($this, 'errorHandle'));
}
function errorHandle($error_no, $error_msg, $file, $line)
{
throw new ErrorException($error_msg, 0, $error_no, $file, $line);
}
function test($x)
{
$this->error();
}
function error() {
$a = $b;
}
}
$err = new ErrorHandle();
$err->test('m');
输出的结果为:
<br />
<b>Fatal error</b>: Uncaught exception 'ErrorException' with message 'Undefined variable: b' in D:\website\localhost\index.php:18
Stack trace:
#0 D:\website\localhost\index.php(18): ErrorHandle->errorHandle()
#1 D:\website\localhost\index.php(15): ErrorHandle->error('m')
#2 D:\website\localhost\index.php(24): ErrorHandle->test()
#3 {main}
thrown in <b>D:\website\localhost\index.php</b> on line <b>18</b><br />
从上面的输出可以看出,参数'm'转递给了ErrorHandle->error();而不是实际上的ErrorHandle->test('m');即:
$err->test('m');
参数相当于移位了,不知道是什么原因.
但是当我安装了xdebug时,发现用它的异常输出中异常跟踪是正确的(xdebug会接管PHP内置的异常输出函数),如下:
( ! )
Fatal error: Uncaught exception 'ErrorException' with message
'Undefined variable: b' in D:\website\localhost\index.php on line 18
( ! ) ErrorException: Undefined variable: b in D:\website\localhost\index.php on line 18
Call Stack
#
Time
Memory
Function
Location
1
0.0499
66464
{main}( )
..\index.php: 0
2
0.0504
67224
ErrorHandle->test( string(1) )
..\index.php: 24
3
0.0505
67432
ErrorHandle->error( )
..\index.php: 15
4
0.0505
67984
ErrorHandle->errorHandle( long , string(21) , string(30) , long , array(0) )
..\index.php: 0
我用的PHP版本是5.2.10,以为是PHP的BUG,但又不确定,所以没有去bugs.php.net上提交,如果发现过类似问题的童鞋们,希望可以讨论一下
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com