更新:在 php 7 中,这不再是一个可捕获的致命错误。而是引发“异常”。一个“异常”(在吓人的引号中),它不是从异常而是错误派生的;它仍然是一个可投掷的,可以用一个普通的尝试捕捉块来处理。查看 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.
编辑:但是你可以“使”它成为一个例外,你可以用一个尝试捕获块来处理
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