php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #10293 object reference vs. true object function passing problem
Submitted: 2001-04-11 14:58 UTC Modified: 2001-11-17 13:01 UTC
From: karkalis at earthling dot net Assigned:
Status: Closed Package: Class/Object related
PHP Version: 4.0.4 OS: FreeBSD 4.1 RELEASE
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: karkalis at earthling dot net
New email:
PHP Version: OS:

 

 [2001-04-11 14:58 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-17 13:01 UTC] 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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 01 21:01:29 2024 UTC