在PHP中,如何检查函数是否存在?

bra*_*adg 46 php function

如何检查my_functionPHP中是否已存在该函数?

Mar*_*iot 91

使用function_exists:

if(function_exists('my_function')){
    // my_function is defined
}
Run Code Online (Sandbox Code Playgroud)

  • 如果要检查类的方法,大多数人会推荐method_exists,但会检测无法从外部访问的受保护/私有方法.在这种情况下,is_callable通常是更好的选择. (15认同)
  • @CaseySoftware - 为方便起见,[这里是文档](http://php.net/manual/en/function.is-callable.php)为``is_callable``,因为这个答案是谷歌的最高结果. (2认同)
  • PHP 库的语义和广泛性非常有趣。“我怎么知道一个函数是否存在?” “`function_exists`” (2认同)

小智 16

http://php.net/manual/en/function.function-exists.php

<?php
  if (!function_exists('myfunction')) {

     function myfunction()
     {
       //write function statements
     }

}
 ?>
Run Code Online (Sandbox Code Playgroud)


T.T*_*dua 7

var_dump( get_defined_functions() );
Run Code Online (Sandbox Code Playgroud)

显示所有现有功能