今天在写一个类时,要将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上提交,如果发现过类似问题的童鞋们,希望可以讨论一下