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
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: 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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 14:01:28 2024 UTC