Bug #76187 [Nab]: Assigning new object in ref-parameter: fn($obj = new stdClass()) raises notice
| From: | requinix@php.net | Date: | Wed, 25 Apr 2018 02:43:39 +0000 |
| Subject: | Bug #76187 [Nab]: 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-214880@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
Updated by: requinix@php.net
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:
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.
Previous Comments:
------------------------------------------------------------------------
[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