在 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模板网!
MySQLi准备好的语句&foreach 循环MySQLi prepared statement amp; foreach loop(MySQLi准备好的语句amp;foreach 循环)
mysqli_insert_id() 是从整个服务器还是从同一用户获Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是从整个服务器还是从同一用户获取记录?)
PHP MySQLi 无法识别登录信息PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 无法识别登录信息)
mysqli_select_db() 需要 2 个参数mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 个参数)
Php mysql pdo 查询:用查询结果填充变量Php mysql pdo query: fill up variable with query result(Php mysql pdo 查询:用查询结果填充变量)
MySQLI 28000/1045 用户“root"@“localhost"的访问MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用户“root@“localhost的访问被拒绝)