Re: trying to do too much with functions?

From: Date: Thu, 02 Sep 2004 17:46:57 +0000
Subject: Re: trying to do too much with functions?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-195630@lists.php.net to get a copy of this message
John Holmes elegantly wrote:
From: "Justin French" <justin.french@indent.com.au>
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. Since I'm using PHP as my templating language (none smarty here!) as well as the programming language. So I've been looking for some alternatives... My main objectives: 1. have all parameters optional 2. have a mixed order for the paramaters Other languages seem to do this well (Ruby, or perhaps RubyOnRails seems good at it), but in PHP I can only see a few options... I'm hoping someone might offer a few more ideas. <? $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?
Have you looked at the "Function Handling Functions"? http://www.php.net/manual/en/ref.funchand.php An array is probably your best bet, though, since you want to have mixed orders. The only way you'd be able to identify what parameter is which is by using the keys of the array, then. ---John Holmes...
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. i should note that I've considered doing this before and I didn't. Now i just use default values of NULL. Jon

« previous php.general (#195630) next »