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: 2010-12-20 15:55 UTC
From: merlinyoda at dotproject dot net Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 5.3.1 OS: N/A
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Feb 21 16:01:27 2020 UTC