php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60544 Variables should also be accepted as default value for user-defined functions
Submitted: 2011-12-16 16:46 UTC Modified: 2014-09-29 00:24 UTC
From: bobwei9 at hotmail dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bobwei9 at hotmail dot com
New email:
PHP Version: OS:

 

 [2011-12-16 16:46 UTC] bobwei9 at hotmail dot com
Description:
------------
I can only use function test ($x = "Hello") {}

I wish to be able to use also variables to define a default value of a function variable like function test ($x = $_SESSION["value"]) {}

If the default-variable doesn't exist and the function isn't called with a parameter, PHP should output a notice that the default variable doesn't exist and assume that it would be NULL.

Test script:
---------------
<?php
function test ($x = $GLOBALS["print"]) { var_dump($x); } // or maybe ($x = $print) ?
$print = "Hello";
test("Bye"); // should output string(3) "Bye"
test(); // should output string(5) "Hello" (the value of $GLOBALS["print"])
$print = "Hi";
test(); // should output string(2) "Hi" (the actual value of $GLOBALS["print"])
unset($print);
test(); // should throw a notice "Undefined index: print" and output NULL
?>

or for a method in a class

<?php
class x
{
public $print = "Hello"
function test ($x = $this->print) { print $x; }
}
$x = new x;
$x->test("Bye"); // should output "Bye"
$x->test(); // should output "Hello" (the value of $x->print)
?>


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-09-22 14:21 UTC] piotrekrogowski at gmail dot com
I wanted to report identical feature request;

+1 for this.
 [2014-09-29 00:24 UTC] bwoebi@php.net
-Status: Open +Status: Wont fix
 [2014-09-29 00:24 UTC] bwoebi@php.net
Closing my own feature request, see also http://stackoverflow.com/a/23667600/2153758 for what I think today about it.

Introducing that would be incredibly bad design, I think.

If you ever think PHP would need that, please don't open a new feature request...
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 15:01:34 2025 UTC