Bug #76187 [Com]: Assigning new object in ref-parameter: fn($obj = new stdClass()) raises notice

From: Date: Sun, 29 Jul 2018 05:13:51 +0000
Subject: Bug #76187 [Com]: Assigning new object in ref-parameter: fn($obj = new stdClass()) raises notice
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216516@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76187&edit=1 ID: 76187 Comment by: a at b dot c dot de Reported by: matthes at leuffen dot de Summary: Assigning new object in ref-parameter: fn($obj = new stdClass()) raises notice Status: Not a bug Type: Bug Package: *General Issues PHP Version: 7.2.4 Block user comment: N Private report: N New Comment: Yes; the whole assignment is what I meant by "the expression"; its value is that of its RHS. Assignment expressions are not variables. In the original, the value is the new object; in your version the value is that of $var (and not, for instance the variable $var itself). And $obj plays no part in the value in either case. Previous Comments: ------------------------------------------------------------------------ [2018-04-25 02:43:38] requinix@php.net It doesn't matter than the RHS of the assignment was an expression - it's the entire assignment itself that's the problem. So even if the RHS is a variable, fn($obj = $var); it will not work. ------------------------------------------------------------------------ [2018-04-25 02:22:18] a at b dot c dot de To be specific, "$obj = new stdClass()" is an expression that evaluates to the value on its right-hand side; as a side effect it also sets the variable on the left to that value. So when it's used in a larger expression, the larger expression gets the value (in this case, a new stdClass object), not the variable. ------------------------------------------------------------------------ [2018-04-05 15:01:05] requinix@php.net $obj=new stdClass() is an expression and expressions cannot be passed by reference. ------------------------------------------------------------------------ [2018-04-05 14:58:30] matthes at leuffen dot de Description: ------------ Notice is falsely triggered. The two examples should be the same. Test script: --------------- <?php ini_set("display_errors", 1); error_reporting(E_ALL); // Function with reference parameter function fn (&$obj) {} fn($obj = new stdClass()); // <- Triggers notice: Only variables should be passed by reference // ... should be the same as ... $obj = new stdClass(); fn($obj); // <- No warning Expected result: ---------------- No notice Actual result: -------------- Notice: Only variables should be passed by reference in xxx ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76187&edit=1

« previous php.bugs (#216516) next »