试图在函数上调用一组参数(Trying to call an array of arguments on a function)
我有一个参数数组传递给一个方法,其中参数的数量是可变的。 我完全不知道怎么做。
class Entity { protected $realObject; public function __call($name, $arguments) { // just call this method on the $property, otherwise redefine the method // error_log("Called $name"); // TODO support variable numbers of arguments $argc = count($arguments); if($argc > 0) { return $this->realObject->$name($arguments[0]); } else { return $this->realObject->$name(); } } }我一直在寻找各种各样的方法,但似乎无法找出将数组转换为变量变量的方法。
I have an array of arguments to pass to a method where the number of arguments is variable. I have absolutely no idea how to do it.
class Entity { protected $realObject; public function __call($name, $arguments) { // just call this method on the $property, otherwise redefine the method // error_log("Called $name"); // TODO support variable numbers of arguments $argc = count($arguments); if($argc > 0) { return $this->realObject->$name($arguments[0]); } else { return $this->realObject->$name(); } } }I've been looking at all sorts of ways of doing it, but can't seem to work out a way of turning an array into a variable of variables.
最满意答案
在PHP> = 5.6 (你应该至少运行它)中有内置的支持 。
例如:
$parameters = ['parameter1', 'parameter2', 'parameter3']; function iAcceptManyParameters(...$parameters) { foreach ($parameters as $parameter) { echo $parameter, "\n"; } } iAcceptManyParameters(...$parameters);你可以看到它在这里工作。
There is built-in support for this in PHP >= 5.6 (which you should be running at the very least).
E.g.:
$parameters = ['parameter1', 'parameter2', 'parameter3']; function iAcceptManyParameters(...$parameters) { foreach ($parameters as $parameter) { echo $parameter, "\n"; } } iAcceptManyParameters(...$parameters);You can see it working here.
更多推荐
发布评论