设置自定义异常处理程序时,error_get_last()在PHP 7中返回NULL [英] error_get_last() returns NULL in PHP 7 when a custom exception handler is set

查看:303
本文介绍了设置自定义异常处理程序时,error_get_last()在PHP 7中返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这花了一些时间才能分解。在这里是:

Ok, this took some time to break it down. Here it is:

有一个包含错误的脚本,本文剩余部分内容如下:

There is an included faulty script which is the following for the remainder of this post:

<?php
$a = 4 // missing semicolon
$b = 2;

然后考虑以下脚本来处理错误。请注意,自定义异常处理程序最初并未注册。

Then consider the following script for handling the error. Note, that the custom exception handler is initially not registered.

<?php

// disable default display of errors
ini_set('display_errors', 0);

// register functions
#set_exception_handler('catchException'); // initially not set
register_shutdown_function('catchError');

// define error function
function catchError(){

  echo "PHP version: ".phpversion();

  if(is_null(error_get_last())) echo "<h1>No errors fetched!</h1>";
  else                          echo "<h1>Error fetched:</h1>";

  var_dump(error_get_last());

}

// define exception function (not used in all examples)
function catchException(){}

// include faulty script
include("D:/temp/faulty.php");



没有自定义异常处理程序的结果



PHP 5和7的结果相同。 error_get_last()函数返回上次发生的错误(屏幕截图)。

现在我们设置自定义函数以取消注释该行

Now we set a custom function uncommenting the line

set_exception_handler('catchException');

在PHP 5中可以正常工作,但是在PHP 7中, error_get_last( )函数返回 NULL 屏幕截图)。

This will work fine in PHP 5, however in PHP 7 the error_get_last() function returns NULL (Screenshot).

这是为什么?尤其令人困惑的是,自定义异常处理程序为空,例如没有成功处理错误。

Why is this? Especially confusing as the custom exception handler is empty, e.g. not "successfully handling" the error.

如何避免这种情况?

祝一切顺利,谢谢提示!

All the best and thanks for hints!

问题(不是真正的问题)是PHP 7抛出了ParseError类型的异常,而不是产生一个错误。因此,最好使用异常处理程序进行处理。做一个很好的异常处理程序以很好地处理异常:

The thing (not really a problem) is that PHP 7 throws an exception of type ParseError rather then producing an error. Thus, it is best handled with an exception handler. Make a nice exception handler to handle the exception well:

function catchException($e){

  echo "<h1>".get_class($e)."</h1>";
  echo $e->getMessage()."<br>";

}


推荐答案

PHP 7引发 ParseError 异常,而不是触发 E_PARSE 类型的错误。如果遇到未捕获的异常,则默认异常处理程序似乎会触发错误。但是,如果将它替换为 set_exception_handler(),除非您自己进行操作,否则它将不再发生。

PHP 7 throws a ParseError exception instead of triggering an error of type E_PARSE. The default exception handler seems to trigger an error if an uncaught exception is encountered. However if you replace it with set_exception_handler() it no longer happens unless you do it yourself.

请参见 PHP文档


PHP 7更改了PHP报告大多数错误的方式。现在,大多数错误不是通过PHP 5使用的传统错误报告机制来报告错误,而是通过引发Error异常来报告。

PHP 7 changes how most errors are reported by PHP. Instead of reporting errors through the traditional error reporting mechanism used by PHP 5, most errors are now reported by throwing Error exceptions.

这篇关于设置自定义异常处理程序时,error_get_last()在PHP 7中返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆