Req #60544 [Opn->Wfx]: Variables should also be accepted as default value for user-defined functions

From: Date: Mon, 29 Sep 2014 00:24:41 +0000
Subject: Req #60544 [Opn->Wfx]: Variables should also be accepted as default value for user-defined functions
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-187741@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=60544&edit=1 ID: 60544 Updated by: bwoebi@php.net Reported by: bobwei9 at hotmail dot com Summary: Variables should also be accepted as default value for user-defined functions -Status: Open +Status: Wont fix Type: Feature/Change Request Package: Scripting Engine problem PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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... Previous Comments: ------------------------------------------------------------------------ [2014-09-22 14:21:36] piotrekrogowski at gmail dot com I wanted to report identical feature request; +1 for this. ------------------------------------------------------------------------ [2011-12-16 16:46:49] 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) ?> ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=60544&edit=1

« previous php.bugs (#187741) next »