php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51409 foreach structure pass by reference scope issue
Submitted: 2010-03-26 22:31 UTC Modified: 2010-03-27 01:43 UTC
From: sramage at nucleuslabs dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.13 OS: FREEBSD 8.0
Private report: No CVE-ID: None
 [2010-03-26 22:31 UTC] 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!
        )

)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-26 22:36 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2010-03-26 22:36 UTC] rasmus@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-27 01:03 UTC] sramage at nucleuslabs dot com
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().
 [2010-03-27 01:43 UTC] rasmus@php.net
Ah, your bug title is a bit misleading then.  How is this any different from:

$foo = array(1,2,3);
$ref = &$foo[2];
$bar = $foo;
$ref = 4;
print_r($bar);

No classes or loops required.  And yes, the reference to the array element 
survives the copy there.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 19:01:33 2024 UTC