【问题标题】:In PHP, how do I check if a function exists?在 PHP 中,如何检查函数是否存在?
【发布时间】:2010-12-04 03:54:42
【问题描述】:

如何检查函数my_function是否已经存在于PHP中?

【问题讨论】:

    标签: php function


    【解决方案1】:

    使用function_exists

    if(function_exists('my_function')){
        // my_function is defined
    }
    

    【讨论】:

    • 如果你想检查类上的方法,大多数人会推荐 method_exists 但它会检测到无法从外部访问的受保护/私有方法。在这种情况下,is_callable 通常是一个更好的选择。
    • @CaseySoftware - 为方便起见,here's the doc 代表 is_callable,因为这个答案是 Google 上的最高结果。
    • PHP 库的语义和广泛性非常有趣。 “我怎么知道一个函数是否存在?” "function_exists"
    • 如果存在这个函数如何检查? $date = new DateTime(); $date->getTimestamp() ?
    • if(!function_exists('my_function')) { }
    【解决方案2】:

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

    <?php
      if (!function_exists('myfunction')) {
    
         function myfunction()
         {
           //write function statements
         }
    
    }
     ?>
    

    【讨论】:

      【解决方案3】:

      如果my_function 在命名空间中:

      namespace MyProject;
      
      function my_function() {
          return 123;
      }
      

      你可以检查它是否存在

      function_exists( __NAMESPACE__ . '\my_function' );
      

      在同一个命名空间中或

      function_exists( '\MyProject\my_function' );
      

      在命名空间之外。

      附:我知道这是一个非常古老的问题,并且 PHP 文档从那时起改进了很多,但我相信人们仍然在这里偷看,这可能会有所帮助。

      【讨论】:

        【解决方案4】:
        var_dump( get_defined_functions() );
        

        显示所有现有功能

        【讨论】:

          【解决方案5】:

          我想指出 kitchin 在 php.net 上指出的内容:

          <?php
          // This will print "foo defined"
          if (function_exists('foo')) {
            print "foo defined";
          } else {
            print "foo not defined";
          }
          //note even though the function is defined here, it previously was told to have already existed
          function foo() {}
          

          如果您想防止致命错误并仅在尚未定义的情况下定义函数,则需要执行以下操作:

          <?php
          // This will print "defining bar" and will define the function bar
          if (function_exists('bar')) {
            print "bar defined";
          } else {
            print "defining bar";
            function bar() {}
          } 
          

          【讨论】:

            【解决方案6】:

            检查多个 function_exists

            $arrFun = array('fun1','fun2','fun3');
            if(is_array($arrFun)){
              $arrMsg = array();
              foreach ($arrFun as $key => $value) {
                if(!function_exists($value)){
                  $arrMsg[] = $value;
                }
              }
              foreach ($arrMsg as $key => $value) {
                echo "{$value} function does not exist <br/>";
              }
            }
            function fun1(){
            }
            
            Output
            
            fun2 function does not exist 
            fun3 function does not exist 
            

            【讨论】:

              猜你喜欢
              • 2014-02-18
              • 2015-07-24
              • 2010-11-05
              • 2014-01-14
              • 2012-10-24
              • 1970-01-01
              • 1970-01-01
              • 2011-06-30
              相关资源
              最近更新 更多