在PHP中可以防止“致命错误:调用未定义的函数”? [英] Is it possible in PHP to prevent "Fatal error: Call to undefined function"?

查看:1126
本文介绍了在PHP中可以防止“致命错误:调用未定义的函数”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,有什么办法可以忽略未定义的函数,而不是抛出浏览器中可见的致命错误?-ie,致命错误:调用未定义的函数



我知道在以下条件中包含所有自定义函数的做法,但是有没有一个编程方式来获得这个效果?

  if(function_exists('my_function')){

// use my_function()here;

}


解决方案

致命错误是致命的。即使您要编写自己的错误处理程序或使用 @ 错误抑制运算符, E_FATAL 错误仍将导致脚本停止执行。



处理此问题的唯一方法是使用 function_exists() (可能 is_callable() ,如上例所示。



对于潜在的(可能的)错误,防御性编码总是更好的主意,只是让错误发生,稍后再处理。 >

编辑 - 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中可以防止“致命错误:调用未定义的函数”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆