|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-07-03 10:17 UTC] bugs dot php dot net at public dot salagir dot com
Description:
------------
In an array, classes become classes references when I use a function member.
Because of that, I don't copy it anymore, I just produce more pointers to it.
(see the '&' in the var dump)
If you comment the magic line, the bug won't appear.
Tested under linux (php 4.1.2 and 4.2.3)
and windows (php 4.3.2)
Reproduce code:
---------------
<?php
class AAAAA {
var $value;
function AAAAA() {}
function show() { return 5; }
}
$A = array(new AAAAA());
$A[0]->show(); /* magic line */
$A[0]->value = 'A';
$B = $A;
$B[0]->value = 'B';
echo $A[0]->value.$B[0]->value;
echo "\n";
var_dump( $A );
?>
Expected result:
----------------
AB
array(1) {
[0]=>
object(aaaaa)(1) {
["value"]=>
string(1) "A"
}
}
Actual result:
--------------
BB
array(1) {
[0]=>
&object(aaaaa)(1) {
["value"]=>
string(1) "B"
}
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 16:00:01 2025 UTC |
The following piece produces a similar result: <?php class foo {} $a = array(new foo()); function show(&$this) { return 5; } show($a[0]); var_dump($a); ?>To copy an array having object elements use unserialize(serialize(myarray)): <?php class test { VAR $test_var = 'this is default value'; } $a = array(new test()); $b = $a; $b[0]->test_var = 'this is set by $b instance'; print_r($a); print_r($b); $c = unserialize(serialize($a)); $c[0]->test_var = 'this is set by $c instance'; echo "---------------------\n"; print_r($a); print_r($b); print_r($c); ?> Be careful! If you have __sleep AND __wakeup functions in your class you could have problem!