|  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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: merlinyoda at dotproject dot net
New email:
PHP Version: OS:


 [2009-12-03 00:48 UTC] merlinyoda at dotproject dot net
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;
define ('CONNECT_SERVER', getHostName());
function connectFunct($user_name, $password, $server = CONNECT_SERVER, $port=3306) {
   //some code
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"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-20 15:55 UTC]
-Package: Feature/Change Request +Package: Scripting Engine problem
 [2021-07-22 12:42 UTC]
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-07-22 12:42 UTC]
There have been no comments or upvotes for more than ten years,
so apparently there is not much interest in this feature.  Thus closing

If someone is still interested in this, please pursue the RFC

[1] <>
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Thu Feb 09 14:03:38 2023 UTC