Bug #17938: Invalid object copy
| From: | jacob at inow8 dot com | Date: | Mon, 24 Jun 2002 08:04:02 +0000 |
| Subject: | Bug #17938: Invalid object copy | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-11875@lists.php.net to get a copy of this message | ||
From: jacob@inow8.com
Operating system: Window 2000
PHP version: 4.2.1
PHP Bug Type: Class/Object related
Bug description: Invalid object copy
The below example shows how $a is copyed (=) into $b but dosent make a
"copy" or dos it?
Its about the prob properti, i asign an object "Obj" to $a->prob, and sets
a new properti, $a->prob->p1, then i run the method $a->prob->PrintObj(),
and copyes $a it into $b, and asign $b->prob->p2 to the cloned version of
$b, and $a->prob->p3 to the original version of $a.
If this sould be right then $a->prob will have p1 and p3, and $b->prob
will have p1 and p2... but because of the PrintObj method thats not the
case... and it has nothing to do with the name and action of the method!
class Obj {
function Obj() {
$this->name = 'a';
}
function PrintObj() {
echo 'Printing...';
}
}
$a = new Obj();
$a->prob = new Obj();
$a->prob->p1 = 'test';
//This is the problem... running a method (any method) before copying!
//Causing a "reference" only to the "prob" properti in the copy version.
$a->prob->PrintObj();
$b = $a;
$b->prob->p2 = 'test';
$a->prob->p3 = 'test';
print_r($a);
print_r($b);
/*
Printing...obj Object
(
[name] => a
[prob] => obj Object
(
[name] => a
[p1] => test
[p2] => test
[p3] => test
)
)
obj Object
(
[name] => b
[prob] => obj Object
(
[name] => a
[p1] => test
[p2] => test
[p3] => test
)
)
*/
$a = new Obj();
$a->prob = new Obj();
$a->prob->p1 = 'test';
//This code dos what i whant, because i dont execute the PrintObj
method????
$b = $a;
$b->prob->p2 = 'test';
$a->prob->p3 = 'test';
print_r($a);
print_r($b);
/*
obj Object
(
[name] => a
[prob] => obj Object
(
[name] => a
[p1] => test
[p3] => test
)
)
obj Object
(
[name] => b
[prob] => obj Object
(
[name] => a
[p1] => test
[p2] => test
)
}
*/
Your sincerely
Jacob Lindborg
--
Edit bug report at http://bugs.php.net/?id=17938&edit=1
--
Fixed in CVS: http://bugs.php.net/fix.php?id=17938&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=17938&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=17938&r=needtrace
Try newer version: http://bugs.php.net/fix.php?id=17938&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=17938&r=support
Expected behavior: http://bugs.php.net/fix.php?id=17938&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=17938&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=17938&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=17938&r=globals