Req #50368 [Opn->Wfx]: Need variable default function param values, undefine(), or "semi-constants"
| From: | cmb@php.net | Date: | Thu, 22 Jul 2021 12:42:06 +0000 |
| Subject: | Req #50368 [Opn->Wfx]: Need variable default function param values, undefine(), or "semi-constants" | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-235258@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=50368&edit=1
ID: 50368
Updated by: cmb@php.net
Reported by: merlinyoda at dotproject dot net
Summary: Need variable default function param values,
undefine(), or "semi-constants"
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: N/A
PHP Version: 5.3.1
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
There have been no comments or upvotes for more than ten years,
so apparently there is not much interest in this feature. Thus closing
as WONTFIX.
If someone is still interested in this, please pursue the RFC
process[1].
[1] <https://wiki.php.net/rfc/howto>
Previous Comments:
------------------------------------------------------------------------
[2009-12-03 00:48:17] merlinyoda at dotproject dot net
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 this bug report at https://bugs.php.net/bug.php?id=50368&edit=1