* Jonathan Haddad <[EMAIL PROTECTED]>: > John Holmes elegantly wrote: > > From: "Justin French" <[EMAIL PROTECTED]> > > > I have a few functions with way too many parameters in them, or > > > functions which have a non-obvious order of parameters which I > > > constantly have to refer to as I'm working. <snip> > > > My main objectives: > > > > > > 1. have all parameters optional > > > 2. have a mixed order for the paramaters <snip> > > > <? $params = array('a'=>'123','b'=>'456'); echo doStuff($params); ?> > > > <?=doStuff(array('a'=>'123','b'=>'456'))?> > > > <?=doStuff('a=123','b=456')?> > > > <?=doStuff('a','123','b','456'); ?> > > > <?=doStuff("a='123' b='456'")?> > > > > > > So, is that it? Have I thought of all possible options?
I come from a perl background, which assumes an array is passed to a subroutine. So, I personally like passing associative arrays to PHP functions/methods, as this gives me very similar functionality. This corresponds to the first two examples given by the OP. However, regarding the following... > > Have you looked at the "Function Handling Functions"? > > > > http://www.php.net/manual/en/ref.funchand.php <snip> > OR, if you really wanted to get crazy, you could do it like objective-C. > > function blah() > { > // contents > } > > call the function like so > > $classobject->function("firstname:jim", "lastname:steve" ); > > within the function, you could call func_get_args, then map your > variables to an array using list($var, $val) = explode( ":", $arg[x] ) > > of course, if you had a : (colon) in either string that exact solution > wouldn't work. but that's not the point of this lesson. Actually, as long as there are no colons in the variable name (key) portion, you're fine -- simply do: list($var, $val) = explode(':', $arg[x], 2); which limits you to two return values from your explode() statement. Unfortunately, what you're left with is: $args = func_get_args(); for ($i = 0; $i < count($args); $i++) { $list($var, $val) = explode(':', $args[$i], 2); $$var = $val; } You could make a function out of the above loop: function get_args($args) { $arg = array(); for ($i = 0; $i < count($args); $i++) { $list($var, $val) = explode(':', $args[$i], 2); $arg[$var] = $val; } return $arg; } function do_something() { $args = func_get_args(); $args = get_args($args); extract($args); // do something now... } But, as you can see, you still end up with a lot of duplication and chances to go wrong -- for instance, what if you forget to include the file that has get_args() in it? This is why I like OOP... :-) -- Matthew Weier O'Phinney | mailto:[EMAIL PROTECTED] Webmaster and IT Specialist | http://www.garden.org National Gardening Association | http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php