php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #21288 PHP does referencing when i want to create a *copy* of an array of objects
Submitted: 2002-12-30 06:52 UTC Modified: 2004-08-03 15:35 UTC
Votes:2
Avg. Score:4.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: empx at gmx dot de Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 4.3.0 OS: Windows XP SP1
Private report: No CVE-ID: None
 [2002-12-30 06:52 UTC] empx at gmx dot de
Hello, i have some problems with understanding the following:

class Test {
var $a;
function Test() {
$this->a = 0;
}
function test2() {
}
}

$a[0] = new Test;
$b = $a;
$a[0]->a = 1;
echo($b[0]->a);

This outputs 0 as i would expect..but:

$a[0] = new Test;
$a[0]->test2();
$b = $a;
$a[0]->a = 1;
echo($b[0]->a);

This outputs 1, and i dont understand this, PHP seems to do some sort of
referencing here, though i dont want any.. $b[0] = $a[0]; works and creates
a real copy, but i was still wondering if $b = $a shouldnt create a copy
aswell instead of this referencing stuff...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-30 08:45 UTC] moriyoshi@php.net
Verified. This is yet another "shallow copy" issue.

See http://bugs.php.net/20993

 [2002-12-30 08:57 UTC] moriyoshi@php.net
It seems more explanation should have been needed...

This problem is due to misleading behavior of array copies. PHP scripting engine doesn't perform deep-copy on any elements of an array while it copies *the container* of them indeed.

This will be fixed in ZendEngine2. Stay tuned.

 [2002-12-30 09:39 UTC] stephan at wanderinghorse dot net
Just FYI, i've reproduced this (using the code from the 
original poster) on PHP 4.2.0 under Linux.
 [2004-04-18 04:15 UTC] php dot devel at homelinkcs dot com
> This will be fixed in ZendEngine2. Stay tuned.  
  
Downloaded, compiled and tested PHP 5.0.0RC1 and all tests 
yielded same result as 4.3.4.  Example: 
 
[josh@joshua php-5.0.0RC1]$ sapi/cli/php 
<?php 
print phpversion()."\n"; 
 
$a[0] = "original"; $b =& $a[0]; $c = $a; $c[0] = "copy"; 
 
print $a[0]."\n"; 
?> 
5.0.0RC1 
copy 
[josh@joshua php-5.0.0RC1]$ php 
<?php 
print phpversion()."\n"; 
 
$a[0] = "original"; $b =& $a[0]; $c = $a; $c[0] = "copy"; 
 
print $a[0]."\n"; 
?> 
4.3.4 
copy 
[josh@joshua php-5.0.0RC1]$
 [2004-06-30 04:13 UTC] moriyoshi@php.net
As explained in bug #20993, this is assumed to be expected behaviour and will unlikely be *fixed* ever.
 [2004-08-03 15:11 UTC] phpbg at cytrax dot de
Ho can I create a *real* copy of an array or to make having no references.

foreach does not create a real copy if the array has references on objects in it.

Is there any function to find out if the array-entry is an object or only a reference?
 [2004-08-03 15:35 UTC] empx at gmx dot de
This is the function i wrote back then to fix my problem, modified a few weeks ago for the php5 object cloning syntax:

function arraycopy(&$array) {
  reset($array);
  while(list($key, $val) = each($array))
  {
    if(is_array($val))
    {
      $array2[$key] = arraycopy($val);
    }
    elseif(is_object($val))
    {
      $array2[$key] = clone $val;
    }
    else
    {
      $array2[$key] = $val;
    }
  }
  return $array2;
}

i'm no php expert, but this worked for me :)
hth
Mike
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 16:01:29 2024 UTC