php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37731 internal reference becomes external
Submitted: 2006-06-07 15:55 UTC Modified: 2006-06-20 14:15 UTC
From: nhorton at gmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.4.2 OS: xp
Private report: No CVE-ID: None
 [2006-06-07 15:55 UTC] nhorton at gmail dot com
Description:
------------
this sure seems like a bug to me. referencing alpha to beta causes a reference to be set up between all further iterations of the object.  delta, without the reference, behaves normally.  i also tried it using foreach loops instead of for loops, same thing happened.  




Reproduce code:
---------------
class one{
     function one($i){
	       $this->beta=$i;
	       $this->alpha=&$this->beta;
								               $this->delta='diamond';
     }
}

for($i=0;$i<11;$i++){
     $one= new one($i);
     $array[]=$one;
}			

for($i=0;$i<11;$i++){
     $first_object=$array[$i];
     $second_object=$first_object;
}
echo "<br>before loop<br>";
print_r($second_object);

for($i=0;$i<11;$i++){
     $object=$array[$i];
     $object->alpha='xxxx';
     $object->delta='spade';
}

echo "<br>after loop<br>";
print_r($second_object);


Expected result:
----------------
expected output:

before loop
one Object ( [beta] => 10 [alpha] => 10 [delta] => diamond )
after loop
one Object ( [beta] => 10 [alpha] => 10 [delta] => diamond )

Actual result:
--------------
output:

before loop
one Object ( [beta] => 10 [alpha] => 10 [delta] => diamond )
after loop
one Object ( [beta] => xxxx [alpha] => xxxx [delta] => diamond )


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-06-20 14:15 UTC] tony2001@php.net
$this->alpha=&$this->beta;
This line makes $this->aplha to point to $this->beta, i.e. they become the same variable, so changing alpha changes beta as well and vice versa.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 16:01:30 2024 UTC