Обновление: в php 7 эта ошибка больше не обнаруживается. Вместо этого выдается «исключение». «Исключение» (в кавычках), которое не является производным от Исключение , но Ошибка ; это все еще Throwable и может обрабатываться с помощью обычного блока try-catch. см https://wiki.php.net/rfc/throwable-interface
1010 * Е.Г. *
<?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.
Старый ответ для версий pre-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.
edit: Но вы можете «сделать» это исключение, которое вы можете обработать с помощью блока 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.';
см .: http://docs.php.net/ErrorException