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

我试图在我的一个类上实现 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 的参数1: : method _ a ()必须是 ClassB 的一个实例,类错误的实例给定..。

是否有可能捕捉到这个错误(因为它说“可捕捉”) ? 如果是,如何捕捉?

60160 次浏览

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

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.constants says:

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.

see also: 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

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