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

问题描述 投票:0回答:1

我正在尝试在我的一个班级上实现 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 的实例...

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

php fatal-error type-hinting
1个回答
117
投票

更新:在 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 之前版本的旧答案:
https://www.php.net/manual/en/errorfunc.constants.php#constant.e-recoverable-error 说:

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

参见:https://www.php.net/manual/en/class.errorexception.php

© www.soinside.com 2019 - 2024. All rights reserved.