Bug #11909 Updated: Object Copied By Reference!!
| From: | brianlmoon@php.net | Date: | Thu, 05 Jul 2001 19:05:26 +0000 |
| Subject: | Bug #11909 Updated: Object Copied By Reference!! | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-59015@lists.php.net to get a copy of this message | ||
ID: 11909
Updated by: brianlmoon
Reported By: nassar@wpi.edu
Old-Status: Open
Status: Bogus
Bug Type: Scripting Engine problem
Operating system:
PHP Version: 4.0.6
Assigned To:
Comments:
This is expected behavior. You are assuming that the object you had in $obj2 should be gone when
you copy $obj to that var. That is not the case. It only changes what $obj2 points to. Take this
case:
$var=3;
$var2=&$var;
unset($var);
echo $var2;
$var2 is still 3. The reference is gone but not the value of the var. That is what makes
references nice.
Brian.
Previous Comments:
---------------------------------------------------------------------------
[2001-07-05 10:54:01] nassar@wpi.edu
When objects that contain objects that are referenced
elsewhere, they are not correctly dereferenced, and strange
side effects result
class Foo{
var $a;
function Foo($value)
{
$this->set($value);
}
function set($value)
{
$this->a = $value;
}
function get()
{
return $this->a;
}
}
class Bar{
var $a;
function Bar($value)
{
$this->a = new Foo($value);
}
function set($value)
{
$this->a->set($value);
}
function get()
{
return $this->a->get();
}
function share($other)
{
$this->a = & $other->a;
}
}
//Objects are initialized
$obj = new Bar(1);
$obj2 = new Bar(2);
//$obj and $obj2 both contain an internal reference to the
//same Foo object, with a value of 2
$obj->share($obj2);
//$obj2 now references a new Bar object with a new
//Foo object containing a value of 3
$obj2 = new Bar(3);
//$obj2 should be a copy of $obj1 and contain a new
//Foo object with a value of 2
$obj2 = $obj;
//Somehow, this also changes the value in $obj2
$obj->set(4);
//In a sane world, this outputs 2
//In reality, we get a 4
echo $obj2->get();
---------------------------------------------------------------------------
ATTENTION! Do NOT reply to this email!
To reply, use the web interface found at http://bugs.php.net/?id=11909&edit=2