(N unknown) $ controller & # 8594; $ action ($ param1, $ param2, $ param3 ... $ paramN);
2 answers
Not sure what you want, but if you want to call a method with an unknown number of parameters, you can use call_user_func_array()
eg:
$result = call_user_func_array(array($controller, $action), $params);
which defines an array like:
array(1, 2, 'a');
would be equivalent to this:
$result = $controller->$action(1, 2, 'a');
You can build an array like this by doing something like below, but I think it's better to use the array in the first place
$param1 = 'Something';
$param2 = 'Test';
$j = 1;
$params = array();
while (isset(${'param' . $j})) {
$params[] = ${'param' . $j};
$j++;
}
print_r($params);
//will output
Array
(
[0] => Something
[1] => Test
)
+2
a source to share
PHP uses very flexible arrays. You can put any data type in them. This creates the need for N parameters. Just use an array as a parameter and skip all the elements (parameters).
If you are using mixed types as parameters you could in a loop check which types of variables using the gettype ($ var) function.
<?php
$params = Array($param1, $param2, $paramN);
$controller->$action($params)
?>
+1
a source to share