php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50368 Need variable default function param values, undefine(), or "semi-constants"
Submitted: 2009-12-03 00:48 UTC Modified: 2021-07-22 12:42 UTC
From: merlinyoda at dotproject dot net Assigned: cmb (profile)
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.3.1 OS: N/A
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
7 + 48 = ?
Subscribe to this entry?

 
 [2009-12-03 00:48 UTC] 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"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-20 15:55 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem
 [2021-07-22 12:42 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-07-22 12:42 UTC] cmb@php.net
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>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 03:01:32 2024 UTC