Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an Array as an Argument

Tags:

arrays

php

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

like image 291
jonilgz Avatar asked Jun 21 '26 11:06

jonilgz


1 Answers

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);
like image 123
AbraCadaver Avatar answered Jun 23 '26 01:06

AbraCadaver