如何捕获“可捕获的致命错误"?在PHP类型提示? [英] How can I catch a "catchable fatal error" on PHP type hinting?

查看:125
本文介绍了如何捕获“可捕获的致命错误"?在PHP类型提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的一个课程中实现PHP5的类型提示,

I am trying to implement Type Hinting of PHP5 on one of my class,

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的实例...

是否有可能捕获到该错误(因为它说可捕获")?如果可以,怎么办?

Is it possible to catch that error(since it says "catchable")? and if yes, how?

推荐答案

更新:在php 7中,这不再是可捕获的致命错误.而是引发了异常".不是源自异常而是源自 Throwable ,可以使用常规try-catch块进行处理.参见 https://wiki.php.net/rfc/throwable-interface

Update: This is not a catchable fatal error anymore in php 7. Instead an "exception" is thrown. An "exception" (in scare quotes) that is not derived from Exception but Error; it's still a Throwable and can be handled with a normal try-catch block. see 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 说:


Old answer for pre-php7 versions:
http://docs.php.net/errorfunc.constants says:

E_RECOVERABLE_ERROR(整数)
可捕获的致命错误.它表明发生了可能是危险的错误,但并未使引擎处于不稳定状态.如果错误未由用户定义的句柄捕获(另请参见 set_error_handler ()),该应用程序因为是E_ERROR而中止.
E_RECOVERABLE_ERROR ( integer )
Catchable fatal error. It indicates that a probably dangerous error occured, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle (see also set_error_handler()), the application aborts as it was an 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块进行处理

edit: But you can "make" it an exception you can handle with a try-catch block

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类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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