Bug #71215 [Csd->Nab]: foreach() with inline assignment doesn't keep references

From: Date: Thu, 24 Dec 2015 21:39:14 +0000
Subject: Bug #71215 [Csd->Nab]: foreach() with inline assignment doesn't keep references
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198210@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71215&edit=1 ID: 71215 Updated by: requinix@php.net Reported by: bugs dot php dot net at ss dot st dot tc Summary: foreach() with inline assignment doesn't keep references -Status: Closed +Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: Linux, OSX PHP Version: 7.0.1 Block user comment: N Private report: N Previous Comments: ------------------------------------------------------------------------ [2015-12-24 20:04:28] bugs dot php dot net at ss dot st dot tc (closing, sorry, typo in test script (I wish I could edit it after the ticked has already been submitted!)) ------------------------------------------------------------------------ [2015-12-24 19:58:42] bugs dot php dot net at ss dot st dot tc Description: ------------ When an inline assignment of array_expression is done right in foreach() statement, all further references to iterated array items seem to become their copies. I believe both PHP 5.6 and 7 are affected, yet still they behave differently. Tested on PHP 5.6.12, 5.6.16, 7.0.0, 7.0.1. Test script: --------------- <?php # source array $source = [ 'subarray' => ['item' => 'old value ---'] ]; # $ref1 is assigned to $source right in the foreach() foreach ( $ref1 = $source as &$subarray ) { $subarray['item'] = 'NEW value'; echo $subarray['item'], PHP_EOL; echo $ref1['subarray']['item'], PHP_EOL; } echo PHP_EOL; # $ref1 is assigned to $source normally, as a separate operation $ref2 = $source; foreach ( $ref2 as &$subarray ) { $subarray['item'] = 'NEW value'; echo $subarray['item'], PHP_EOL; echo $ref2['subarray']['item'], PHP_EOL; } echo PHP_EOL; ######################################## # similar to $source array, but implemented using a function function getSource() { return [ 'subarray' => ['item' => 'old value ---'] ]; } # $ref1 is assigned to return value of getSource() right in the foreach() foreach ( $ref3 = getSource() as &$subarray ) { $subarray['item'] = 'NEW value'; echo $subarray['item'], PHP_EOL; echo $ref3['subarray']['item'], PHP_EOL; } echo PHP_EOL; # $ref4 is a "normal" reference to a result of getSource() $ref4 = getSource(); foreach ( $ref4 as &$subarray ) { $subarray['item'] = 'NEW value'; echo $subarray['item'], PHP_EOL; echo $ref3['subarray']['item'], PHP_EOL; } Expected result: ---------------- both PHP 5 and 7: NEW value NEW value NEW value NEW value NEW value NEW value NEW value NEW value Actual result: -------------- PHP 7: NEW value old value --- NEW value NEW value NEW value old value --- NEW value old value --- PHP 5: NEW value old value --- NEW value NEW value NEW value NEW value NEW value NEW value ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71215&edit=1

« previous php.bugs (#198210) next »