views:

149

answers:

2

(N is unknown)

$controller->$action($params);

must be

$controller->$action($param1, $param2, $param3... $paramN);

+1  A: 

PHP uses very flexible arrays. You can put any data types in them. That eleminates the need of N parameters. Just use a array as parameter and loop through all elements (parameters).

If you use mixed types as parameters you could in the loop check what types the variables are by using the gettype($var) function.

<?php
    $params = Array($param1, $param2, $paramN);
    $controller->$action($params)
?>
Baversjo
+2  A: 

Not really 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()

for example:

$result = call_user_func_array(array($controller, $action), $params);

which given an array like:

array(1, 2, 'a');

would be equivalent to this:

$result = $controller->$action(1, 2, 'a');

You could build such an array by doing something like below, but I think it would be better to use an 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
)
Tom Haigh