在 PHP 中,有什么方法可以忽略未定义的函数,而不是抛出在浏览器中可见的致命错误?—即,致命错误:调用未定义的函数>
我知道有将所有自定义函数包装在如下条件中的做法,但是有没有一种编程方式来获得这种效果?
if (function_exists('my_function')) {//在这里使用 my_function();}
没有.致命错误是致命的.即使您要编写自己的错误处理程序或使用 @
错误抑制运算符,E_FATAL
错误仍会导致脚本停止执行.
处理这个问题的唯一方法是使用 function_exists()
(可能还有 is_callable()
为很好的措施)如上面的例子.
围绕潜在的(可能的?)错误进行防御性编码总是比让错误发生并在以后处理它更好的主意.
EDIT - php7 改变了这种行为,未定义的函数/方法是可捕获的异常.
In PHP, is there any way that I can ignore functions that are undefined instead of throwing a fatal error that is visible in the browser?—i.e., Fatal error: Call to undefined function
I know that there is the practice of wrapping all custom functions in a conditional as below, but is there a programmatic way to get this effect?
if (function_exists('my_function')) {
// use my_function() here;
}
No. Fatal errors are fatal. Even if you were to write your own error handler or use the @
error suppression operator, E_FATAL
errors will still cause the script to halt execution.
The only way to handle this is to use function_exists()
(and possibly is_callable()
for good measure) as in your example above.
It's always a better idea to code defensively around a potential (probable?) error than it is to just let the error happen and deal with it later anyway.
EDIT - php7 has changed this behavior, and undefined functions/methods are catchable exceptions.
这篇关于在 PHP 中是否可以防止“致命错误:调用未定义的函数"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!