Bug #76187 [Com]: Assigning new object in ref-parameter: fn($obj = new stdClass()) raises notice
| From: | a at b dot c dot de | Date: | Wed, 25 Apr 2018 02:22:21 +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-214879@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:
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.
Previous Comments:
------------------------------------------------------------------------
[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