Edit report at http://bugs.php.net/bug.php?id=51409&edit=1
ID: 51409 User updated by: sramage at nucleuslabs dot com Reported by: sramage at nucleuslabs dot com Summary: foreach structure pass by reference scope issue Status: Bogus Type: Bug Package: Arrays related Operating System: FREEBSD 8.0 PHP Version: 5.2.13 New Comment: Everything you wrote was perfectly correct and I agree completely with it but that is not the problem. The problem is that a member of a class is being modified from outside the class without directly referencing it. The array $bananas was *copied* to the property $monkey->bananas before the reference was modified. $monkey->bananas appears to be the same array in memory as $bananas (which is how PHP is designed to save on memory usage) at the point which $bananas gets changed I would expect $monkey->bananas and $bananas to diverge but they don't. This is a very minor issue since it would be bad programming practice to leave a pass by reference variable floating around without using unset(). Previous Comments: ------------------------------------------------------------------------ [2010-03-26 22:36:25] ras...@php.net This makes perfect sense. After your first foreach loop, $banana is a reference to the last element in your $bananas array. Anything you do to that $banana variable is going to affect that last element. So, in your coconut foreach, you are now asking PHP to assign the first element of the coconut array to the last element of your bananas array which is exactly the output you are getting. No bug here. ------------------------------------------------------------------------ [2010-03-26 22:31:41] sramage at nucleuslabs dot com Description: ------------ When an ampersand operator is used in a foreach language construct The pass by reference variable causes *copies* of the parent array used in the foreach loop to be modified in any future use of the initial pass by reference variable. This is very hard to explain clearly but see the test script below for clarification it is a very simple test script.' Similar bugs like this have been reported and documentation outlines a simple workaround that does work and solves my problem. but I figured I would report this problem as *copies* of the array are affected and it seems kind of strange. Test script: --------------- class Monkey{ public $bananas=array(); public function AddBananas($bananas){ $this->bananas=$bananas; }} $bananas=array(); $bananas['banana1']=array('color'=>'yellow','size'=>'big'); $bananas['banana2']=array('color'=>'green','size'=>'small'); $coconuts=array(); $coconuts['coconut1']['size']='tiny'; $coconuts['coconut2']['size']="I'm a"; $monkey=new Monkey(); foreach($bananas as $key=>&$banana){ $banana['id']=$key+1; } $monkey->AddBananas($bananas); foreach($coconuts as $banana){ $banana['type']="coconut!"; } print_r($monkey->bananas); Expected result: ---------------- Array ( [banana1] => Array ( [color] => yellow [size] => big [id] => 1 ) [banana2] => Array ( [color] => green [size] => small [id] => 2 ) ) Actual result: -------------- Array ( [banana1] => Array ( [color] => yellow [size] => big [id] => 1 ) [banana2] => Array ( [size] => I'm a [type] => coconut! ) ) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=51409&edit=1