疑为PHP BUG:将PHP的错误封装为ErrorException出现的问题

在使用PHP自定义错误处理函数时遇到参数错位的问题,通过对比发现安装xdebug后异常跟踪正常。涉及PHP版本5.2.10。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在写一个类时,要将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-&gt;errorHandle()
#1 D:\website\localhost\index.php(15): ErrorHandle-&gt;error('m')
#2 D:\website\localhost\index.php(24): ErrorHandle-&gt;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
#TimeMemoryFunctionLocation
10.049966464{main}( )..\index.php:0
20.050467224ErrorHandle->test( string(1) )..\index.php:24
30.050567432ErrorHandle->error( )..\index.php:15
40.050567984ErrorHandle->errorHandle( long, string(21), string(30), long, array(0) )..\index.php:0

 

我用的PHP版本是5.2.10,以为是PHP的BUG,但又不确定,所以没有去bugs.php.net上提交,如果发现过类似问题的童鞋们,希望可以讨论一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值