How can I use a function's default arguments but change one of the end ones in the list?
| From: | Daevid Vincent | Date: | Thu, 05 Feb 2009 23:50:42 +0000 |
| Subject: | How can I use a function's default arguments but change one of the end ones in the list? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-287776@lists.php.net to get a copy of this message | ||
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...
<?php
function SQL_QUERY($sql, $parameters = null, $showSQL = false,
$showErrors = true, $execute = true, $noHTML = false, $profile = 0)
{
var_dump($sql, $parameters, $showSQL, $showErrors, $execute, $noHTML,
$profile);
}
?>
<pre>
<?php SQL_QUERY('SELECT * FROM foo WHERE bar = ?'); ?>
</pre>
<hr>
<pre>
<?php SQL_QUERY('SELECT * FROM foo WHERE bar = ?', array('beep'),
true); ?>
</pre>
<hr>
<pre>
<?php SQL_QUERY('SELECT * FROM foo WHERE bar = ?', array('beep'),
$execute=false); ?>
</pre>
outputs:
-----------------------------------------------------------------------
string(31) "SELECT * FROM foo WHERE bar = ?"
NULL
bool(false)
bool(true)
bool(true)
bool(false)
int(0)
-----------------------------------------------------------------------
string(31) "SELECT * FROM foo WHERE bar = ?"
array(1) { [0]=> string(4) "beep" }
bool(true)
bool(true)
bool(true)
bool(false)
int(0)
-----------------------------------------------------------------------
string(31) "SELECT * FROM foo WHERE bar = ?"
array(1) { [0]=> string(4) "beep" }
bool(false)
bool(true)
bool(true) <-- I would have expected this one to be bool(false)
bool(false)
int(0)
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?!? :-(