【问题标题】:How can I catch a "catchable fatal error" on PHP type hinting?如何在 PHP 类型提示中捕获“可捕获的致命错误”?
【发布时间】:2011-01-28 22:28:12
【问题描述】:

我正在尝试在我的一个班级上实现 PHP5 的类型提示,

class ClassA {
    public function method_a (ClassB $b)
    {}
}

class ClassB {}
class ClassWrong{}

正确用法:

$a = new ClassA;
$a->method_a(new ClassB);

产生错误:

$a = new ClassA;
$a->method_a(new ClassWrong);

可捕获的致命错误:传递给 ClassA::method_a() 的参数 1 必须是 ClassB 的实例,给定 ClassWrong 的实例...

是否有可能捕获该错误(因为它说“可捕获”)?如果是,如何?

【问题讨论】:

  • 供将来参考:Exceptions in the engine (for PHP 7) - 从 PHP 7 开始,可能会捕获致命错误。这也适用于此处讨论的 “可捕获的致命错误” (E_RECOVERABLE_ERROR),因为这些错误从 PHP 7 开始就会被捕获..

标签: php fatal-error type-hinting


【解决方案1】:

更新:在 php 7 中,这不再是一个可捕获的致命错误。而是抛出一个“异常”。不是从Exception 而是Error 派生的“异常”(用引号括起来);它仍然是 Throwable 并且可以使用普通的 try-catch 块来处理。见https://wiki.php.net/rfc/throwable-interface

例如

<?php
class ClassA {
  public function method_a (ClassB $b) { echo 'method_a: ', get_class($b), PHP_EOL; }
}
class ClassWrong{}
class ClassB{}
class ClassC extends ClassB {}


foreach( array('ClassA', 'ClassWrong', 'ClassB', 'ClassC') as $cn ) {
    try{
      $a = new ClassA;
      $a->method_a(new $cn);
    }
    catch(Error $err) {
      echo "catched: ", $err->getMessage(), PHP_EOL;
    }
}
echo 'done.';

打印

catched: Argument 1 passed to ClassA::method_a() must be an instance of ClassB, instance of ClassA given, called in [...]
catched: Argument 1 passed to ClassA::method_a() must be an instance of ClassB, instance of ClassWrong given, called in [...]
method_a: ClassB
method_a: ClassC
done.

php7 之前版本的旧答案:
http://docs.php.net/errorfunc.constants 说:

E_RECOVERABLE_ERROR(整数)
可捕获的致命错误。它表示发生了可能危险的错误,但并未使引擎处于不稳定状态。如果错误未被用户定义的句柄捕获(另见set_error_handler()),应用程序将中止,因为它是一个 E_ERROR。

另见:http://derickrethans.nl/erecoverableerror.html

例如

function myErrorHandler($errno, $errstr, $errfile, $errline) {
  if ( E_RECOVERABLE_ERROR===$errno ) {
    echo "'catched' catchable fatal error\n";
    return true;
  }
  return false;
}
set_error_handler('myErrorHandler');

class ClassA {
  public function method_a (ClassB $b) {}
}

class ClassWrong{}

$a = new ClassA;
$a->method_a(new ClassWrong);
echo 'done.';

打印

'catched' catchable fatal error
done.

编辑:但是您可以“使”它成为一个可以使用 try-catch 块处理的异常

function myErrorHandler($errno, $errstr, $errfile, $errline) {
  if ( E_RECOVERABLE_ERROR===$errno ) {
    echo "'catched' catchable fatal error\n";
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
    // return true;
  }
  return false;
}
set_error_handler('myErrorHandler');

class ClassA {
  public function method_a (ClassB $b) {}
}

class ClassWrong{}

try{
  $a = new ClassA;
  $a->method_a(new ClassWrong);
}
catch(Exception $ex) {
  echo "catched\n";
}
echo 'done.';

见:http://docs.php.net/ErrorException

【讨论】:

  • 当然,这非常像一个致命错误,除非您查看服务器日志时找不到它。谢谢 php :/
  • 换句话说,您无法捕获可捕获的错误。太棒了!
  • @Paul 是什么让你得出这个结论?
  • 哦,我只是说它在传统意义上是不可捕获的(使用 try/catch 块)。那天我只是对 PHP 感到不爽,所以当我发现它在完全不同的意义上是“可捕获的”时,我觉得有必要发表评论。没有什么反对你的精彩回答(事实上我赞成);我所有的愤怒都是针对 PHP 本身!
  • 我有 PHP 7.0.12 并且无法使用 TypeErrorErrorException\Throwable 中的任何一个来捕获 Catchable fatal error: Object of class stdClass could not be converted to string - 有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 2019-04-20
  • 1970-01-01
  • 2011-01-20
  • 1970-01-01
  • 2014-03-31
  • 1970-01-01
  • 2022-12-10
相关资源
最近更新 更多