php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #12856 dynamic default function parameter not possible
Submitted: 2001-08-20 06:36 UTC Modified: 2009-10-22 14:21 UTC
Votes:6
Avg. Score:2.7 ± 0.9
Reproduced:3 of 4 (75.0%)
Same Version:1 (33.3%)
Same OS:2 (66.7%)
From: marcus dot boerger at post dot rwth-aachen dot de Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.0.6 OS:
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: marcus dot boerger at post dot rwth-aachen dot de
New email:
PHP Version: OS:

 

 [2001-08-20 06:36 UTC] marcus dot boerger at post dot rwth-aachen dot de
Hi all,

Why is it not possible to have dynamic default function parameters?

This would give the possibility to execute any code snippet when the default value will be used in a function code.

At current time you can only define
function f( $v=<statis_val>) {
  return $v;
}

return f();
what would result in: <static_val>

BUT sometimes there must be a dynamic part. For example when dealing with times. Consider the following:
function t( $v=null) {
if ($v===null) $v=time();
return date( "r", $v);
}
return t();
what would return the current time formatted.

With dynamic defaults this would enable the following:
function t($v=time()) {
return date( "r", $v);
}
return t();

Additionally that *should* be used to give access to global variables:
$d = time();
function t($v=$d) {
return date( "r", $v);
}
return t(); // formatted time of script start..or given time

Another good idea would be to execute the variables from within context:
$d = 1;
function t($v=$d) {
return $v;
}
function t2() {
$d=2;
return t();
}
function t3($v=$GLOBALS['d']) {
return $v;
}
return t();   // --> 1
return t2();  // --> 2
return t3();  // --> 1

greetings marcus

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-22 11:01 UTC] felix at littlestarmedia dot com
It is possible to have dynamic default values for functions.

Set the default value to something that will never be passed to it and then test if the variable has that value in it. If it does then apply your dynamic value from within the function.
If it doesn't have that value then clearly a value has been passed.

E.g.

function CreateYearDropDown($year = 1000.398) {
	
     If ($year = 1000.398) { $year = date(Y); }

     //Now Create the month drop down

}
 [2009-10-22 11:12 UTC] felix at littlestarmedia dot com
eek - sorry, that code should have been:
(double ==)

function CreateYearDropDown($year = 1000.398) {
	
     If ($year == 1000.398) { $year = date(Y); }

     //Now Create the month drop down

}
 [2009-10-22 11:19 UTC] felix at littlestarmedia dot com
Ok, I should really test my code properly before posting here - sorry.

This version definitely works!

function CreateYearDropDown($year = 1000.398) {
	
    if ($year == 1000.398) { 
	$year = date(m); 
    }

    //Now Create the month drop down

}
 [2009-10-22 14:21 UTC] johannes@php.net
We are sorry, but we can not support PHP 4 related problems anymore.
Momentum is gathering for PHP 6, and we think supporting PHP 4 will
lead to a waste of resources which we want to put into getting PHP 6
ready.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 08:01:29 2024 UTC