hob*_*all 94 php type-hinting fatal-error
我试图在我的一个类上实现PHP5的类型提示,
class ClassA {
public function method_a (ClassB $b)
{}
}
class ClassB {}
class ClassWrong{}
Run Code Online (Sandbox Code Playgroud)
正确用法:
$a = new ClassA;
$a->method_a(new ClassB);
Run Code Online (Sandbox Code Playgroud)
产生错误:
$a = new ClassA;
$a->method_a(new ClassWrong);
Run Code Online (Sandbox Code Playgroud)
可捕获的致命错误:传递给ClassA的参数1 :: method_a()必须是ClassB的一个实例,ClassWrong的实例给出...
我是否可以知道是否有可能发现错误(因为它说"可捕获")?如果是,怎么样?
谢谢.
Vol*_*erK 110
更新:这不再是一个可捕获的致命错误在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.';
Run Code Online (Sandbox Code Playgroud)
版画
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.
Run Code Online (Sandbox Code Playgroud)
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.';
Run Code Online (Sandbox Code Playgroud)
版画
'catched' catchable fatal error
done.
Run Code Online (Sandbox Code Playgroud)
编辑:但你可以"使"它成为你可以使用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.';
Run Code Online (Sandbox Code Playgroud)
请参阅:http://docs.php.net/ErrorException