传递数组作为参数

时间:2016-04-25 15:32:01

标签: php arrays

我正在制作一个控制器,它从一个类调用一个不同的方法,我希望尽可能地使它成为通用的。我目前有这个代码:

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

echo json_encode($res);

我想要的是将数组作为参数传递给我的Geolocation类。

我从下一个帖子中提出了一个想法:Passing an Array as Arguments, not an Array, in PHP

问题在于" splat operator" ...对我不起作用,可能是因为它不是一个功能。有没有相关的解决方案?

PHP 5.6.20

1 个答案:

答案 0 :(得分:2)

您链接问题的第一个答案显示call_user_func_array(),因此使用对象方法执行:

$res = call_user_func_array(array($geo, $method), $params);