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

From: Date: Mon, 22 Sep 2014 14:21:36 +0000
Subject: Req #60544 [Com]: 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-187650@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 Comment by: piotrekrogowski at gmail dot com Reported by: bobwei9 at hotmail dot com Summary: Variables should also be accepted as default value for user-defined functions Status: Open Type: Feature/Change Request Package: Scripting Engine problem PHP Version: Irrelevant Block user comment: N Private report: N New Comment: I wanted to report identical feature request; +1 for this. Previous Comments: ------------------------------------------------------------------------ [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 (#187650) next »