arrays - call_user_func_array passing additional params to method -
in app.php
file have this:
for url one: http://mywebroot/myapp/param1/param2
# leftover in $url, set params, else empty. $this->params = $url ? array_values($url) : []; print_r($this->params); // gives me dumped array values should // can see array ( [0] => param1 [1] => param2 ) // trying pass array controller: // // # call our method requested controller, passing params if method call_user_func_array([ $this->controller, $this->method ], $this->params); // don't worry `$this->controller`, // `$this->method` part, end calling method class bellow.
in controller file have:
class home extends controller { // here expecting catch params // public function index($params = []){ var_dump($params); // gives `string 'param1' (length=6)`? array? // not relevant question # request view, providing directory path, , sending along vars $this->view('home/index', ['name' => $user->name]); }
so question is, why in controller dont have $params
array, first element of array. if instead do:
public function index($param1, $param2){
i have of them, want flexibility in terms of how many of params get.
you want use call_user_func
not call_user_func_array
call_user_func
takes first parameter callable
, rest send parameters function. while call_user_func_array
expects 2 parameters - first 1 callable
second 1 array parameters of called function. see following example:
function my_func($one, $two = null) { var_dump($one); var_dump($two); } call_user_func('my_func', array('one', 'two')); call_user_func_array('my_func', array('one', 'two'));
first (call_user_func
) dump:
array(2) { [0]=> string(3) "one" [1]=> string(3) "two" } null
while call_user_func_array
result in:
string(3) "one" string(3) "two"
hope helps
Comments
Post a Comment