3

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.

zt1983811
  • 1,011
  • 3
  • 14
  • 34
Tom Schmitz
  • 62
  • 2
  • 7
  • 1
    PHP 5.4 is nearly two years out of support and dangerously insecure to run as a result. That said, the old approach was using http://php.net/func_get_args instead. – ceejayoz Jun 21 '17 at 19:30
  • Thanks i'll take a look at func_get_args. And yeah i know it has been out of support for quit a while now, but my teacher doesn't what to update his local server :/ – Tom Schmitz Jun 21 '17 at 19:32
  • 1
    Either explode to an array and pass `0` and `1` or use `call_user_func_array()`. – AbraCadaver Jun 21 '17 at 19:33

2 Answers2

2

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);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

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)

hanshenrik
  • 19,904
  • 4
  • 43
  • 89