#10293 [Com]: object reference vs. true object function passing problem
| From: | bear-black710 at hotmail dot com | Date: | Tue, 20 Jul 2004 15:00:33 +0000 |
| Subject: | #10293 [Com]: object reference vs. true object function passing problem | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-62780@lists.php.net to get a copy of this message | ||
ID: 10293
Comment by: bear-black710 at hotmail dot com
Reported By: karkalis at earthling dot net
Status: Closed
Bug Type: Class/Object related
Operating System: FreeBSD 4.1 RELEASE
PHP Version: 4.0.4
New Comment:
<a href=http://d-latin-balloonfoot.da.ru>bear
black</a>
Previous Comments:
------------------------------------------------------------------------
[2001-11-17 13:01:30] mfischer@php.net
Thats the way it works in PHP4. Objects are treated as normal variables
and therefore also copied (means, really copied) when assigning or
passing around so $foo = &new class; is the right way to avoid this.
Closed.
------------------------------------------------------------------------
[2001-04-11 14:58:51] karkalis at earthling dot net
here is my script:
<?php
class Tbug {
function add($f) {
$this->stuff[] = $f;
}
function spew() {
foreach($this->stuff as $key => $f) echo "stuff is [$f]<BR>";
}
}
function & addtobug($f, &$bug) {
$bug->add($f);
return($bug);
}
// using first choice we have problems, second choice works, why the
diff?
//$bug = new Tbug();
$bug = &new Tbug();
echo "call1<BR>";
$bug = &addtobug("a", &$bug);
echo "call2<BR>";
$bug = &addtobug("b", &$bug);
echo "call3<BR>";
$bug = &addtobug("c", &$bug);
$bug->spew();
?>
The "work around" is to use the second choice of assigning $bug to the
reference of the "new" Object. This does not seem inuitive to me. Why
does this code not work otherwise?
thanks,
tonys.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=10293&edit=1