tags:

views:

90

answers:

6

I'm wondering if there are any syntactic problems calling a function that only has one argument with multiple arguments. For example:

function foobar( $biff ) {
// do stuff...
}

... and then call the function:

echo foobar( 'Hello', 'world', '!!!' )

I believe that $biff === 'Hello', right?

And then I will use array_slice( func_get_args(), 1 ); to play with the args 'world' and '!!!'.

Is this right or is this not recommended?

P.S. I am working inside a function wrapper, which holds a lot of other functions containing various numbers of arguments.

A: 

A better approach would be to define all expected arguments, and have optional arguments have a default value:

function foo($a, $b = '', $c = 1) { ... }

This way it's much more obvious what parameters can be given to a function.

Jani Hartikainen
That's not possible, unfortunately. I'm working inside a function wrapper.
Jeff
A: 

your array_slice approach will only work in newer php versions (since 5.3)

otherwise your code is fine, but why don't you just put all three params in the function prototype?

knittl
I have it working on 5.2 right now. Mainly concerned if it's okay to call extra arguments with a function that only asks for one.
Jeff
at least the php.net page says it only works since 5.3. calling with extra arguments doesn’t hurt, but as others said: you won’t have code completion in your ide and such
knittl
+1  A: 

The idea should work, and it's perfectly valid : it's called Variable-length argument lists ; and it's not either "right" or "wrong".

If you need it, well, use it ;-)

One small problem, though, with the way you are using func_get_arg : you cannot use it as a function parameter -- see the note at the bottom of the manual page.


The problem I see with this kind of idea is that you won't get hinting in your IDE -- which is not so nice when writing code :-(

So I would use this only when really necessary (ie, when you really don't know how many parameters you need), even if it works fine.


Another option, if you know the mamimum number of possible arguments, would be to use default / optionnal parameters.

Pascal MARTIN
A: 

PHP: func_get_args

Note: Because this function depends on the current scope to determine parameter details, it cannot be used as a function parameter. If this value must be passed, the results should be assigned to a variable, and that variable should be passed.

Marko
A: 

I guess that'll work. PHP has a page which describes how this works, lists all of the variadic args related functions, and has some examples: http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list

rascher
A: 

It will work, but as @Pascal Martin says, assign the arguments returned by func_get_arg to a variable before manipulating it. To get some code assist in your IDE, you can simply use PhpDoc notation:

/**
 * This function takes $biff and does yadayadayada. Additionally, more arguments 
 * can be passed in and will be used to ...
 * @param string $biff This argument will ...
 * @param string [$foo] Optional. Any number of arguments can be passed in and ..
 * @return string The return value of this function is $biff and any other 
 * parameters ...
 */
function foobar( $biff ) {
// do stuff...
}

This notation will give you code assist in Eclipse PDT, Zend Studio and probably a good few others.

PatrikAkerstrand