更新:这不再是php
7中可捕获的致命错误。相反,抛出了“异常”。不是从Exception而是Error派生的“exception”(用引号引起来);它仍然是Throwable,可以使用常规的try-catch块进行处理
例如
<?phpclass 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: ClassBmethod_a: ClassCdone.
php7之前版本的旧答案:
http :
//docs.php.net/errorfunc.constants 说:
E_RECOVERABLE_ERROR(整数)
可捕获的致命错误。它表明发生了可能是危险的错误,但没有使引擎处于不稳定状态。如果错误未由用户定义的句柄捕获另请参见[set_error_handler(),则应用程序将中止,因为它是E_ERROR。
例如
function myErrorHandler($errno, $errstr, $errfile, $errline) { if ( E_RECOVERABLE_ERROR===$errno ) { echo "'catched' catchable fatal errorn"; 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 errordone.
编辑:但是您可以“使其”成为您可以使用try-catch块处理的异常
function myErrorHandler($errno, $errstr, $errfile, $errline) { if ( E_RECOVERABLE_ERROR===$errno ) { echo "'catched' catchable fatal errorn"; 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 "catchedn";}echo 'done.';
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)