代码之家  ›  专栏  ›  技术社区  ›  jay

PHP call_user_func与仅调用函数

  •  96
  • jay  · 技术社区  · 16 年前

    我确信对此有一个非常简单的解释。这两者之间有什么区别:

    function barber($type){
        echo "You wanted a $type haircut, no problem\n";
    }
    call_user_func('barber', "mushroom");
    call_user_func('barber', "shave");
    

    function barber($type){
        echo "You wanted a $type haircut, no problem\n";
    }
    barber('mushroom');
    barber('shave');
    
    8 回复  |  直到 8 年前
        1
  •  79
  •   Kai    16 年前

    call_user_func

        2
  •  31
  •   Lucas Oman    16 年前

    虽然你可以这样调用变量函数名:

    function printIt($str) { print($str); }
    
    $funcname = 'printIt';
    $funcname('Hello world!');
    

    function someFunc() {
      $args = func_get_args();
      // do something
    }
    
    call_user_func_array('someFunc',array('one','two','three'));
    

    分别调用静态和对象方法也很方便:

    call_user_func(array('someClass','someFunc'),$arg);
    call_user_func(array($myObj,'someFunc'),$arg);
    
        3
  •  15
  •   Brienne Schroth    16 年前

    call_user_func 选项存在,因此您可以执行以下操作:

    $dynamicFunctionName = "barber";
    
    call_user_func($dynamicFunctionName, 'mushroom');
    

    哪里 dynamicFunctionName

        4
  •  7
  •   pivotal    16 年前

    使用PHP 7,您可以在任何地方使用更好的变量函数语法。它与静态/实例函数一起工作,并且可以接受参数数组。更多信息请访问 https://trowski.com/2015/06/20/php-callable-paradox

    $ret = $callable(...$params);
    
        5
  •  3
  •   uingtea    10 年前

    我想它对于调用一个你事先不知道名字的函数是有用的。.. 类似于:

    switch($value):
    {
      case 7:
      $func = 'run';
      break;
      default:
      $func = 'stop';
      break;
    }
    
    call_user_func($func, 'stuff');
    
        6
  •  1
  •   mils    8 年前

    user 这意味着它适用于多个用户,在核心引擎中创建修改而不进行编辑是有用的。

    <?php
    /* main.php */
    
    require("core.php");
    require("my_plugin.php");
    
    the_content(); // "Hello I live in Tasikmalaya"
    

    <?php
    /* core.php */
    
    $listFunc = array();
    $content = "Hello I live in @@@";
    
    function add_filter($fName, $funct)
    {
        global $listFunc;
        $listFunc[$fName] = $funct;
    }
    
    function apply_filter($funct, $content)
    {
        global $listFunc;
        foreach ($listFunc as $key => $value)
        {
            if ($key == $funct and is_callable($listFunc[$key]))
            {
                $content = call_user_func($listFunc[$key], $content);
            }
        }
        echo $content;
    }
    
    function the_content()
    {
      global $content;
      $content = apply_filter('the_content', $content);
      echo $content;
    }
    

    ....

    <?php
    /* my_plugin.php */
    
    function changeMyLocation($content){
      return str_replace('@@@', 'Tasikmalaya', $content);
    }
    
    add_filter('the_content', 'changeMyLocation'); 
    
        7
  •  0
  •   SilentGhost    16 年前

        8
  •  -1
  •   ThomasRedstone    8 年前

    使用名称空间时,call_user_func()是运行事先不知道名称的函数的唯一方法,例如:

    $function = '\Utilities\SearchTools::getCurrency';
    call_user_func($function,'USA');
    

    如果你的所有函数都在同一个命名空间中,那么就不会有这样的问题,因为你可以使用这样的东西:

    $function = 'getCurrency';
    $function('USA');
    

    编辑: 在@Jannis说我错了之后,我做了更多的测试,运气不太好:

    <?php
    namespace Foo {
    
        class Bar {
            public static function getBar() {
                return 'Bar';
            }
        }
        echo "<h1>Bar: ".\Foo\Bar::getBar()."</h1>";
        // outputs 'Bar: Bar'
        $function = '\Foo\Bar::getBar';
        echo "<h1>Bar: ".$function()."</h1>";
        // outputs 'Fatal error: Call to undefined function \Foo\Bar::getBar()'
        $function = '\Foo\Bar\getBar';
        echo "<h1>Bar: ".$function()."</h1>";
        // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()'
    }
    

    您可以在此处看到输出结果: https://3v4l.org/iBERh

    推荐文章