【问题标题】:PHP7 set_exception_handler and set_error_handler callbacks are mixedPHP7 set_exception_handler 和 set_error_handler 回调混用
【发布时间】:2016-05-15 00:37:08
【问题描述】:

似乎我在 php7.0.3 中发现了一些错误,但我不确定问题是否在我身边,所以我决定先写在这里

我最近将我的服务器 php 版本更新为 php7.0.3,当然还有关于不推荐使用的方法等的错误,我正在一一修复它们,直到我来到这个

未捕获的 TypeError:传递给 Debug::_exceptionHandler() 的参数 1 必须是 Exception 的实例,在 SomePath\Debug.php:518 中给出的错误实例 堆栈跟踪:#0 [内部函数]:Debug::_exceptionHandler(对象(错误))#1 {main} 抛出

我检查了Debug 类,发现set_exception_handlerset_error_handlerfunction enable() 中调用

public static function enable( ..some params.. ){
    ...
    set_exception_handler(array(__CLASS__, '_exceptionHandler'));
    set_error_handler(array(__CLASS__, '_errorHandler'));
}

// where _exceptionHandler function defined as
public static function _exceptionHandler(Exception $exception)
{ ... }

// and _errorHandler defined as
public static function _errorHandler($severity, $message, $file, $line, $context)
{ ... }

实际上错误对我来说很清楚,它告诉它不能将Error 类型对象传递给_exceptionHandler,因为它除了Exception 类型,但问题为什么试图将它传递给_exceptionHandler 而不是_errorHandler,因为它也定义了?是错误还是我在 errorexception 处理程序的定义中遗漏了什么?

注意:我尝试使用 set_error_handler 将其作为第二个参数传递 E_ALL 等,但注意帮助...

我也收到了错误,应该由_errorHandler 处理,但由_exceptionHandler 处理是

未捕获的错误:调用 SomePath 中未定义的函数 set_magic_quotes_runtime()...

有人可以帮我解决这个问题吗?

谢谢

【问题讨论】:

    标签: php error-handling exception-handling php-7


    【解决方案1】:

    PHP 7 中的异常层次结构发生了变化,如果您查看 manual page for set_exception_handler 您会注意到:

    从 PHP 7 开始,大多数错误都是通过抛出错误异常来报告的,这些异常也会被处理程序捕获。 Error 和 Exception 都实现了 Throwable 接口。这是自 PHP 7 以来的处理程序签名:

    void handler ( Throwable $ex );
    

    可以在here 找到 PHP 7 中异常层次结构更改的详细信息。

    【讨论】:

    • 其实我查了set_exception_handler页面,确实注意到了这条信息,现在我明白了,谢谢
    猜你喜欢
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    相关资源
    最近更新 更多