|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 19 21:00:01 2025 UTC |
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