如何在 PHP 类型提示上捕获“可捕获的致命错误”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2468487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:35:12  来源:igfitidea点击:

How can I catch a "catchable fatal error" on PHP type hinting?

phpfatal-errortype-hinting

提问by hoball

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

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

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

class ClassB {}
class ClassWrong{}

Correct usage:

正确用法:

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

producing error:

产生错误:

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

Catchable fatal error: Argument 1 passed to ClassA::method_a() must be an instance of ClassB, instance of ClassWrong given...

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

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

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

回答by VolkerK

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 Exceptionbut Error; it's still a Throwableand can be handled with a normal try-catch block. see https://wiki.php.net/rfc/throwable-interface

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

E.g.

例如

<?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.';

prints

印刷

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.


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

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

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

see also: http://derickrethans.nl/erecoverableerror.html

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

e.g.

例如

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.';

prints

印刷

'catched' catchable fatal error
done.

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

编辑:但是您可以“使其”成为可以使用 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.';

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

请参阅:http: //docs.php.net/ErrorException