Adam Ramadhan
Adam Ramadhan

Reputation: 22810

turn array to vars?

$controller->$method($this->params);

ok the problem is that my $this->params is an array, is there something in php that can we do so it will be like

$controller->$method($params1,$params2,$untilllastparams); ?

thanks for looking in

Adam Ramadhan

Upvotes: 1

Views: 101

Answers (3)

MikeMurko
MikeMurko

Reputation: 2233

You could try using the eval and "implode" functions (http://php.net/manual/en/function.implode.php). Basically implode your array as a comma delimited string (this will represent your method parameters) and then build a string like so,

$implodedString = implode(",", $this->params);
eval("$controller->$method($implodedString);")

It's likely not the most ideal solution, and I don't have any way to test it right now, but give it a go.

Upvotes: 0

Adam Hupp
Adam Hupp

Reputation: 1573

You are looking for call_user_func_array(). In your example this would look like:

call_user_func_array(array($controller, $method), $this->params);

http://php.net/manual/en/function.call-user-func-array.php

Upvotes: 3

JP19
JP19

Reputation:

I assume that controller is some library class and you are looking for a different interface. One solution is to define a wrapper function:

function my_wrapper(ControllerClass &$controller, &$params1, &$params2, &$untilllastparams) {
  // create array from parameters
  $controller->$method($param_array);
};

Upvotes: 0

Related Questions