php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #8661 a copy of the internal array elements is sometimes not made
Submitted: 2001-01-11 16:27 UTC Modified: 2001-06-12 12:40 UTC
From: lgordon at idiotbrain dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.4 OS: freeBSD
Private report: No CVE-ID: None
 [2001-01-11 16:27 UTC] lgordon at idiotbrain dot com
<?php
// buggy.php


// If I leave both A1 and A2 commented out, the code works fine.
// If I uncomment A1 only, the code works as expected
// If I uncomment A2 only, the code does not work as expected


class Element
{
    var $val;
    function set_value($newval)
    {
        $this->val = $newval;
    }
}

class Owner
{
    var $elementArray;
    function dump()
    {
        foreach ($this->elementArray as $index => $value)
        {
            print("index [$index] value [$value->val]<BR>");
        }
    }
}

$myOwner = new Owner();
$myOwner->elementArray[0] = new Element;

// -- A1 --
// this works as expected
$myOwner->elementArray[0]->val = 10;

// -- A2 --
// if this line is uncommented the code does not work as expected
//$myOwner->elementArray[0]->set_value(10);

print("dump myOwner<BR>");
$myOwner->dump();
print("<BR>");

print("make a copy of myOwner<BR>");
$copyOwner = $myOwner;
print("<BR>");

print("dump copyOwner<BR>");
$copyOwner->dump();
print("<BR>");

print("change copyOwner index 0 to 999999<BR>");

$copyOwner->elementArray[0]->val = 999999;

print("dump copyOwner<BR>");
$copyOwner->dump();
print("<BR>");

print("dump myOwner<BR>");
$myOwner->dump();
print("<BR>");

?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-01-11 16:41 UTC] lgordon at idiotbrain dot com
This might be the same as 8130, but I'm not sure.

 [2001-01-11 16:46 UTC] lgordon at idiotbrain dot 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:55 UTC] lgordon at idiotbrain dot 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-02-28 00:21 UTC] lgordon at idiotbrain dot com
My bad, my email address is larry@idiotbrain.com

Please detail on a fix for this bug.

Thanks,
Larry
 [2001-03-15 09:20 UTC] 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-03-16 03:58 UTC] lgordon at idiotbrain dot com
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.
 [2001-04-28 15:29 UTC] jmoore@php.net
dup of 8130
 [2001-04-28 15:31 UTC] jmoore@php.net
dup of 8130
 [2001-06-12 12:40 UTC] jmoore@php.net
This is a feature not a bug :P (IE it wont be fixed anytime soon).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 11:01:28 2024 UTC