3

I'm making a controller, which calls a different method from a class and I want to make it as generic as possible. I currently have this code:

//More code up, just showing the important code part
$geo = new Geolocation();
$res = $geo->{$method}(...$params);

echo json_encode($res);

What I want is pass an array as arguments to my Geolocation class.

I took an idea from the next thread: Passing an Array as Arguments, not an Array, in PHP

The problem is that "splat operator" ... is not working for me, probably because it's not a function. Is there any related solution?

PHP 5.6.20

Community
  • 1
  • 1
jonilgz
  • 226
  • 1
  • 6

1 Answers1

2

The first answer for the question you link shows call_user_func_array(), so with an object method do:

$res = call_user_func_array(array($geo, $method), $params);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87