PHP 5.4.17 alternative for the “… operator”

强颜欢笑 提交于 2020-02-15 22:51:33

问题


I was wondering if someone may know an alternative to the PHP 5.6.x and higher ... operator (or splat operator I believe its called).

What i'm currently doing in my PHP 7 version is:

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

The callAction() function takes 2 parameters callAction($controller, $action) but now I need to downgrade the code to PHP 5.4.17.


回答1:


Though the splat operator ... is similar to call_user_func_array():

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

I think it would make more sense to pass the required arguments:

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



回答2:


i think the equivalent PHP5 code would be call_user_func_array(array($this,'callAction'),(explode('@', $this->routes["authControllers"][$this->routes["uri"][$uri]])));

edit: but if the callAction always takes exactly 2 arguments, you could just do

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

but if thats the case, idk why the php7 code bothers with the ... at all, i thought that was for variable number of arguments? (like call_user_func_array is for. for an example of a function that takes a variable number of arguments, see var_dump)



来源:https://stackoverflow.com/questions/44684728/php-5-4-17-alternative-for-the-operator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!