PHP 4.0 Bug #8661 Updated: a copy of the internal array elements is sometimes not made
| From: | lgordon at idiotbrain dot com | Date: | Fri, 16 Mar 2001 08:58:55 +0000 |
| Subject: | PHP 4.0 Bug #8661 Updated: a copy of the internal array elements is sometimes not made | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-49046@lists.php.net to get a copy of this message | ||
ID: 8661
User Update by: lgordon@idiotbrain.com
Status: Analyzed
Bug Type: Scripting Engine problem
Description: a copy of the internal array elements is sometimes not made
I don't agree with this. The zend engine only does a shallow copy in this particular instance,
and it is a bug. If the array is not accessed through a method in the object, the copy is performed
correctly. Please re-analyze this.
Previous Comments:
---------------------------------------------------------------------------
[2001-03-15 09:20:44] stas@php.net
Zend copies arrays and objects "shallow" (for performance
reasons). The workaround is to implement "clone" method.
This probably should be built-in function in Zend.
---------------------------------------------------------------------------
[2001-02-28 00:21:05] lgordon@idiotbrain.com
My bad, my email address is larry@idiotbrain.com
Please detail on a fix for this bug.
Thanks,
Larry
---------------------------------------------------------------------------
[2001-01-11 16:55:42] lgordon@idiotbrain.com
The workaround that I have had to implement is doing an explicit clone method (similar to a copy
constructor) and when making a copy, say
$new = $old->clone();
instead of
$new = $old;
example implementation:
class Element
{
var $value;
function &clone()
{
$obj = new Element();
$obj=>value = $this->value;
}
}
class X
{
var $array;
function &clone()
{
$obj = new X();
foreach($this->array as $index => $value)
{
$obj->array[$index] = $value->clone();
}
return $obj;
}
}
---------------------------------------------------------------------------
[2001-01-11 16:46:44] lgordon@idiotbrain.com
Yes, this is the same, if I change the method to
do_nothing() in the Element class and print out a message,
the same behavior happens. The behavior is that if I call a method on an array object after the
array has been copied, the copy now refers to the original. Also, if the method is called on the
original object, the copy is then linked with the original.
---------------------------------------------------------------------------
[2001-01-11 16:41:44] lgordon@idiotbrain.com
This might be the same as 8130, but I'm not sure.
---------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view the rest of the comments,
please view the bug report online.
Full Bug description available at: http://bugs.php.net/?id=8661