如何判断 PHP 致命错误何时实际上是隐藏的语法错误,以及它们何时发生?
考虑以下 PHP 类:
class someObject {
public function broken(){
return isset($this->something()) ? 'worked' : 'didnt';
}
public function something(){
return true;
}
public function notBroken(){
print('worked');
}
}
假设我现在这样做:
$obj= new someObject();
$obj->broken();
考虑到您无法将函数调用传递给 isset()(它是通过引用),我希望这样因致命错误而失败: PHP Fatal error: Can't use method return value in write context
这很好,并且符合预期。
但是,假设我现在这样做:
$obj= new someObject();
$obj->notBroken();
考虑到我在此执行中的任何位置都没有遇到 broken()
,并且 broken()
中出现错误> 是一个致命错误(而不是解析错误),我不希望正常输出“worked”。 错误!它仍然会生成致命错误。
问题:
除了不编写有错误的代码之外,是否还有其他不是解析错误但仍会触发运行时错误的错误?我只知道:PHP 致命错误:无法在写入上下文中使用方法返回值
。有什么方法可以检测到这些错误吗? 这种类型的错误有专门的名称吗?
Considering the following PHP class:
class someObject {
public function broken(){
return isset($this->something()) ? 'worked' : 'didnt';
}
public function something(){
return true;
}
public function notBroken(){
print('worked');
}
}
Let's say I now do:
$obj= new someObject();
$obj->broken();
Considering you can't pass a function call to isset(), (it's by-reference), I expect this to fail with a fatal error: PHP Fatal error: Can't use method return value in write context
This is fine, and expected.
However, let's say I now do:
$obj= new someObject();
$obj->notBroken();
Considering I'm not hitting the broken()
anywhere in this execution, and the error in broken()
is a Fatal Error (and not a Parse error), I wouldn't expect the normal output of "worked". FALSE! It still generates the Fatal Error.
Question:
Aside from just not writing code that has errors, are there any other errors that are not Parse Errors but still trigger a runtime error? I only know about: PHP Fatal error: Can't use method return value in write context
. Is there any way to detect these errors?
Is there a special name for this type of error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这种特定行为的原因可能是 isset() 是一种语言构造,而不是在运行时解释的普通函数。所以按理说这是一个解析错误。
我对此没有深入的了解,而且我不知道此类错误是否有特定的名称。
The reason for this specific behaviour is probably that
isset()
is a language construct and not a normal function that gets interpreted at runtime. So it stands to reason this is kind of a parse error.I have no deep insight in this though, and I don't know whether this class of errors has a specific name.
这些是“编译错误”,当编译器遇到语法上有效但“无法编译”的构造时,会抛出这些错误。转到 http://svn.php.net/viewvc /php/php-src/trunk/Zend/zend_compile.c 并搜索“E_COMPILE_ERROR” - 有很多。
These are "compile errors", thrown by the compiler when it encounters a syntactically valid but "uncompilable" construct. Go to http://svn.php.net/viewvc/php/php-src/trunk/Zend/zend_compile.c and search for "E_COMPILE_ERROR" - there are quite a few.