如何捕获PHP类型暗示的“可捕获的致命错误”?
I am trying to implement Type Hinting of PHP5 on one of my class,
我正在尝试在我的一个类上实现PHP5的类型提示,
class ClassA {
public function method_a (ClassB $b)
{}
}
class ClassB {}
class ClassWrong{}
Correct usage:
正确使用方法:
$a = new ClassA;
$a->method_a(new ClassB);
producing error:
生产错误:
$a = new ClassA;
$a->method_a(new ClassWrong);
Catchable fatal error: Argument 1 passed to ClassA::method_a() must be an instance of ClassB, instance of ClassWrong given...
可捕获的致命错误:参数1传递给ClassA::method_a()必须是ClassB的实例,ClassWrong的实例给定…
May I know if it is possible to catch that error(since it says "catchable")? and if yes, how?
我能知道是否有可能捕获那个错误(因为它说“可捕获”)吗?如果是的,怎么了?
Thank you.
谢谢你!
1 个解决方案
#1
101
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
更新:在php 7中,这不再是一个可捕获的致命错误。相反,抛出一个“异常”。一个“异常”(在吓人的引号中),它不是来自异常而是错误;它仍然是一个可投掷的,可以用一个普通的try-catch块来处理。参见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:
php7版本的老答案是:http://docs.php.net/errorfunc.constants:
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
参见: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
编辑:但是您可以“使”它成为一个异常,您可以使用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.';
see: http://docs.php.net/ErrorException
参见:http://docs.php.net/ErrorException
更多相关文章
- ThinkPHP5开发Api接口简单实例
- 尽管“SQL语法错误”消息仍然成功执行
- 使用curl加载xsl页面会返回实际的基数错误
- Yourphp系统发生错误
- 在PHP中,为什么没有显示解析错误?
- phpnow1.5.6如何设置404错误页面?
- php异常和错误处理
- 关于一个单例模式的问题,这样写为什么返回的是DB实例,而不是一个PD
- APMServ 在 Win7 下出现“APMServ-Apache 服务因 函数不正确。