From: merlinyoda at dotproject dot net
Operating system: N/A
PHP version: 5.3.1
PHP Bug Type: Feature/Change Request
Bug description: Need variable default function param values, undefine(), or
"semi-constants"
Description:
------------
Somewhat picking up on Bug #8577 here as I see some potentially legitimate
uses for an "unset" function or some functional equivalent as a way to
temporarily set a static value based on an expression. The replies in the
last bug were a bit flippant on both sides so I hope to get a better
resolution as I see this as a potential step towards another degree of
untapped flexibility and ability to minimize code.
The following function definitions are not valid in PHP:
function connectFunct($user_name, $password, $server = getHostName(),
$port=3306) {
//some code
}
function connectVar($user_name, $password, $server =
$_SERVER["SERVER_NAME"], $port=3306) {
//some code
}
at present the only workaround is to have a "flag" value and reassign the
variable as needed like so:
function connectFunct($user_name, $password, $server = null, $port=3306)
{
if ($server === null) {
$server = getHostName();
}
//some code
}
function connectVar($user_name, $password, $server = null, $port=3306) {
if ($server === null) {
$server = $_SERVER["SERVER_NAME"];
}
//some code
}
However the need may easily arise where the "flag" value chosen becomes a
value that triggers some other behavior (e.g. null should be an erroneous
value if passed). If a constant could be redefined or undefined to more
accurately reflect the current state of the value of function call or
variable it would allow for a less "hackish" workaround.
Example using "undefine()":
if (defined('CONNECT_SERVER')) {
$prev_connect_val = CONNECT_SERVER;
undefine('CONNECT_SERVER');
}
define ('CONNECT_SERVER', getHostName());
function connectFunct($user_name, $password, $server = CONNECT_SERVER,
$port=3306) {
//some code
}
undefine('CONNECT_SERVER');
if (is_set($prev_connect_val)) {
define ('CONNECT_SERVER', $prev_connect_val);
}
Alternately, you could have another class of constants which are designed
for the purpose of temporary value storage (i.e. "semi-constants" mentioned
in summary):
Example using "semi-constant" function define_temp():
define_temp ('CONNECT_SERVER', getHostName());
function connectFunct($user_name, $password, $server = CONNECT_SERVER,
$port=3306) {
//some code
}
undefine_temp('CONNECT_SERVER'); //may not be necessary depending on
implementation of "semi-constants"
--
Edit bug report at http://bugs.php.net/?id=50368&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=50368&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=50368&r=trysnapshot53
Try a snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=50368&r=trysnapshot60
Fixed in SVN:
http://bugs.php.net/fix.php?id=50368&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=50368&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=50368&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=50368&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=50368&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=50368&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=50368&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=50368&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=50368&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=50368&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=50368&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=50368&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=50368&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=50368&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=50368&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=50368&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=50368&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=50368&r=mysqlcfg