views:

66

answers:

4

Does PHP has such a feature?

+4  A: 
TiuTalk
A: 

Use the function func_get_args(). It will give you an array of all the parameters passed to that function.

nickf
A: 

Sure, make a function with no parameters, call it with whatever you want, and inside the function use func_get_args() to get the actual arguments.

Max Shawabkeh
you can still have named parameters.
Tor Valamo
A: 

Prehaps a better way is to pass one argument, that is actually an array of arguments. eg:

function my_sum($data){
    $sum = 0;
    foreach($sum as $value){
        $sum+=$value;
    }
    return $sum;
}

echo my_sum(array(1, 2, 5, 109, 10231));

I often use an associative array to pass arguments to a function. This is very usefull if the arguments being submitted are optional, or change in nature. eg:

$data = array(
    'date' => '10/4/06',
    'name' => 'biggles',
    'occupation' => 'pilot'
);
add_person($data);

This will also allow for better error detection. if you have many many arguments, it is quite possible to get them in the wrong order. plus if you extend or modify teh function it is more likely to keep working with existing code.

Bingy
"better" is reliant on the situation. if you're making something like a `sprintf` function, `func_get_args` is a very good solution.
nickf