![]() |
Functions with Endless Amounts of Arguments
Sometimes we need to create functions that require an indefinite and variable amount of arguments. Creating a function to cater for these arguments may seem straight-forward; I've seen several people who have created functions with five or so arguments and if they required a sixth argument at any stage during their project's development, they added the sixth argument to the function and carried on as normal.
Consider the function below that somebody has put together to construct an array from the arguments that are passed into the function: php Code:
This will work perfectly. However, you are not planning for future expansion. Suppose in a week's time you want to create an array with four arguments, what will you do then? Most people would simply add another argument and not look for a more long-term solution to the predicament. Luckily, there is a fairly straight-forward solution, and that is to use a series of native PHP functions. You do not have to explicitly specify the arguments but rather add the arguments in the function call and let PHP do the rest. See what this looks like now we have adapted our code: php Code:
All this does is takes the arguments that we specified in the function call, and return them as an array. There's nothing more to it than that! As we do not know how many arguments we will be requiring, func_get_args takes all the arguments and returns them as an array. Our function call looks like this:php Code:
We can add as many arguments as we like and the function will return us an array containing those arguments. To take this a step further, we can specify explicit arguments and interpret them differently. Even when using func_get_args, we may still place arguments in to the function declaration, like so:PHP Code:
func_num_args which returns an integer value informing you how many arguments have been specified, and func_get_arg which returns a single argument based on the argument offset you pass it. Incidentally, we begin the $iIndex at 1 to bypass the $iLimit argument. Our two out of three fruits are then returned as an array when we call the function:php Code:
And all works well! Using these series of functions definitely spares you from having to return to your function add more arguments if and when you need them. By using func_get_args you can specify limitless arguments and the function will carry on working regardless. If you're already wise enough and realise how to use func_get_args then you'll no doubt be happy to never return to that function again to add extra arguments. It's up to you to spread the word - life just got a little bit easier! |
This could come in handy some day :).
|
Nice tip you got there,
This line : PHP Code:
PHP Code:
PHP Code:
|
You're quite right, Morishani! Well pointed out. There is no excuse for calling a function more than once unless its result changes every single time you require it.
|
Nice example, func_num_args() isnt very widely known/used. A VERY competant php programmer, a friend of mine, was trying to do something similar a while back, and was doing all sorts of parsing to get the variables... He was amazed when he found out about this.
Really odd how unknown this is :) |
Also I didn't know of this function. It will - indeed - come in handy some day!
Thanks for sharing. |
Quote:
Thanks! |
I see a lot of people getting around this by passing in arrays to their functions - which is all fine and dandy in its right place, but it's not a workaround solution, in my opinion. If the arguments differ then in my opinion they should be as different arguments, not just different items in an array.
|
I've been wondering how to do this for ages, thanks.
|
| All times are GMT. The time now is 11:00 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0