Re: How can I use a function's default arguments but change one of the end ones in the list?

From: Date: Fri, 06 Feb 2009 01:12:03 +0000
Subject: Re: How can I use a function's default arguments but change one of the end ones in the list?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-287784@lists.php.net to get a copy of this message
Daevid Vincent wrote: > Is there a way to use the default values of a function without > specifying every single one until the parameter you want to modify in > PHP5 ? > > I don't see it here, but feel this would be very useful indeed. > http://www.php.net/manual/en/functions.arguments.php > > So given a function that takes seven parameters, I want to change one of > them and leave the other defaults alone... > > The above function call doesn't error out on me, it just seems it > doesn't do anything either :-\ > > So it seems I have to do this verboseness (AND know what the default > values are to begin with too): > > SQL_QUERY('SELECT * FROM foo WHERE bar = ?', array('beep'), false, true, > false, false); > > Just to change one default parameter?!? :-( > What you are wanting to do is not possible. Back in the day, I worked on a project that used our own variation of this type of functionality. We had this function that helped a little function alternate(&$a, $b) { return $a = ( $a ? $a : $b ); } function query($sql, $params=array()) { alternate($params, array()); extract($params); alternate($parameters, null); alternate($showSQL, false); alternate($showErrors, true); alternate($execute, true); alternate($noHTML, false); alternate($profile, 0); # Do your thing... } This allowed for us to include/exclude whatever we wanted from the it is a little long winded to get to what you are looking for ( I think ) But it worked for us. -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare

« previous php.general (#287784) next »