百科狗-知识改变命运!
--

function_exists() - php 选项信息函数

百变鹏仔1年前 (2023-11-21)阅读数 10#技术干货
文章标签函数

function_exists()

(PHP 4, PHP 5, PHP 7)

如果给定的函数已经被定义就返回TRUE

说明

function_exists(string $function_name): bool

在已经定义的函数列表(包括系统自带的函数和用户自定义的函数)中查找$function_name。

参数

$function_name

函数名,必须为一个字符串。

返回值

如果$function_name存在且的确是一个函数就返回TRUE,反之则返回FALSE

Note:

function_exists() - php 选项信息函数

对于语法结构的判断,例如include_once和echo将会返回FALSE

范例

Example #1function_exists()的例子

注释

Note:

当本配置或者编译或编译选项禁用某函数时,该函数名也可能存在(image就是一个现成的例子)

参见

  • method_exists() 检查类的方法是否存在
  • is_callable() 检测参数是否为合法的可调用结构
  • get_defined_functions() 返回所有已定义函数的数组
  • class_exists() 检查类是否已定义
  • extension_loaded() 检查一个扩展是否已经加载
You can use this function to conditionally define functions, see: http://php.net/manual/en/functions.user-defined.php
For instance Wordpress uses it to make functions "pluggable." If a plugin has already defined a pluggable function, then the WP code knows not to try to redefine it.
But function_exists() will always return true unless you wrap any later function definition in a conditional clause, like if(){...}. This is a subtle matter in PHP parsing. Some examples:

Prints:
 foo defined
 defining bar
 calling bar
 calling baz
 PHP Fatal error: Call to undefined function qux()
Any oddities are probably due to the order in which you include/require files.
It should be noted that the function_exists check is not relative to the root namespace. This means that the namespace should be appended to the check: 
PHP supports nested function based on certain criteria.
Please look over the code.
function Media(){ }
function Audio()
{
  echo "Plugged Audo 5.1:
"; function Volume() { echo "Volume controls:
"; function Equalizer() { echo "Equalize Bands:
"; } } } //Call to nested functions Audio(); Volume(); Equalizer(); if(function_exists('Volume')): echo "TRUE"; else: echo "FALSE"; endif; Case 1: //Result :Works Well -------- Audio(); Volume(); Equalizer(); Case 2: //Results Notice Error. Root function Audio must be called first. -------- Volume(); Case 3: //Results Error. Root function Volume must be called. -------- Audio(); Equalizer(); Note : The nested function should be called based on their order used. In our example when Audio is not called and instantly when we try to call Volume puts under error. Even though there is an possibility to use nested functions in PHP. It looks overhead to do so. Better to avoid in logical ground of script. Tested on PHP 5.5.32
Functions within a function are better off as anonymous returns from create_function(), unless you want to be able to call it elsewhere.
However, I have used this in skinning: I use alert_box() to display certain errors, like a faulty SQL query. This simply calls display_alert(), which is defined in my skin scripts. However, alert_box() is sometimes called before I know which skin to load, so it has its own functionality which it uses if function_exists('display_alert') returns false.
This is not going to go down as you might expect it should (even if you play smart and require/include_once):

The above will issue a "Cannot redeclare readfile()" fatal error if readfile was disabled with disable_functions.
I, too, was wondering whether is_callable or function exists is faster when checking class methods. So, I setup the following test:

This gives the output :
is_callable = TRUE, function_exists = FALSE
Did 10000 is_callables in 0.0640790462494 seconds
Did 10000 function_exists in 0.0304429531097 seconds
So the fact that function_exists is twice as fast is slightly over shadowed by the fact that it doesn't work on class methods, at least not as far as I can tell.
If you use suhosin.executor.func.blacklist instead of disabled_functions in your php.ini, function_exists will return true for a disabled function. I used this to have the same beahviour with suhosin.executor.func.blacklist and disabled_functions: 
To prevent direct calls to included files i use the following technique.
In the main file create an empty function with a random name. Like so:

Then check for the existence of this function within your include:

Simple but effective.
This can be used to conditionally define a user function. In this sense, it can act as a sort of inline include_once(). 
For example, suppose you have a function A that calls function B. B is only used inside function A and is never called from anywhere else in the script. It's logical (and perfectly legal in PHP) to define B inside of A's definition, like so: 

Without the function_exists test, you would get a fatal error the second time you called A, as PHP would think you were trying to redefine B (not legal in PHP). The placement of the test is also important. Since the if block is executed sequentially, like any other block of code, it must come before any call to the function defined within.
I stumbled over the same problem as "eddiec" (users not able or not willing to use "_once"-suffixes).
A possible alternative explanation for the behavior:
If a file is included, it is possibly parsed every include-time.(?)
While parsing, every function in global scope is tried to register. THIS gets wrong, when multiple times included, and it produces an error.
If functions are defined within block scopes, their registration seems to be delayed until execution of such a block. Thus, not the function "function_exists" functions wrong, but simply the philosophy of the interpreter produces such results.
Thus, the same effect can be achieved by simply putting block braces around the contents of an include_once file:
if (function_exists('function_in_question')) return;
{
  function function_in_question(...)
  {
    ...
  }
  ...other stuff
}
...which is equivalent to...
if (!function_exists('function_in_question'))
{
  function function_in_question(...)
  {
    ...
  }
  ...other stuff
}
I would like to comment on the following post:
A note of caution: function_exists() appears to be case-insensitive (at least as of PHP 4.3.8). e.g.:
 
I believe that function calls itself are case insensitve, so this function is returning a valid truth. PHP doesn't care about cases.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)