RT
功能1:
$class->$func()
Run Code Online (Sandbox Code Playgroud)
功能2:
//Simple callback
call_user_func($func)
//Static class method call
call_user_func(array($class,$func))
//Object method call
$class = new MyClass();
call_user_func(array($class, $func));
Run Code Online (Sandbox Code Playgroud)
有区别吗?我想看看源代码(https://github.com/php/php-src)我们应该做什么?
call_user_func_array在性能方面非常慢,这就是为什么在许多情况下你想要使用显式方法调用.但是,有时你想传递作为数组传递的任意数量的参数,例如
public function __call($name, $args) {
    $nargs = sizeof($args);
    if ($nargs == 0) {
        $this->$name();
    }
    elseif ($nargs == 1) { 
        $this->$name($args[0]);
    }
    elseif ($nargs == 2) { 
        $this->$name($args[0], $args[1]);
    }
    #...
    // you obviously can't go through $nargs = 0..10000000, 
    // so at some point as a last resort you use call_user_func_array
    else { 
        call_user_func_array(array($this,$name), $args);
    }
}
Run Code Online (Sandbox Code Playgroud)
我要检查$nargs最多5个(PHP中的函数通常不太可能接受超过5个参数,所以在大多数情况下我们将直接调用方法而不使用call_user_func_array哪个方法对性能有好处)
结果$class->method($arg)是一样的call_user_func_array(array($class,'method'), array($arg)),但第一个更快.