首页 文章

PHP 5.4.17替代“...运算符”

提问于
浏览
0

我想知道是否有人可能知道PHP 5.6.x及更高版本 ... 运算符的替代品(或者我相信它的splat运算符) .

我目前在PHP 7版本中所做的是:

$this->callAction(
...explode('@', $this->routes["authControllers"][$this->routes["uri"][$uri]])
);

callAction() 函数需要2个参数 callAction($controller, $action) ,但现在我需要将代码降级到PHP 5.4.17 .

2 回答

  • 0

    虽然splat运算符 ... 类似于 call_user_func_array()

    call_user_func_array(array($this,'callAction'),
                         explode('@', $this->routes["authControllers"][$this->routes["uri"][$uri]]));
    

    我认为传递必需的参数会更有意义:

    list($controller, $action) = explode('@', $this->routes["authControllers"][$this->routes["uri"][$uri]]);
    $this->callAction($controller, $action);
    
  • 0

    我认为等效的PHP5代码将是 call_user_func_array(array($this,'callAction'),(explode('@', $this->routes["authControllers"][$this->routes["uri"][$uri]])));

    编辑:但是如果callAction总是采用2个参数,你可以这样做

    $args=explode('@',$this->routes["authControllers"][$this->routes["uri"][$uri]]));
    $this->callAction($args[0],$args[1]);
    

    但如果是这样的话,idk为什么php7代码与 ... 困扰,我认为这是为了可变数量的参数? (就像call_user_func_array是for . 对于一个带有可变数量参数的函数的例子,参见 var_dump

相关问题