php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #32137 Objects are not being passed by reference
Submitted: 2005-02-28 20:39 UTC Modified: 2005-04-27 15:54 UTC
From: tigr at mail15 dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.0.3 OS: Windows 98
Private report: No CVE-ID: None
 [2005-02-28 20:39 UTC] tigr at mail15 dot com
Description:
------------
I make one object instance. Then, pass it to another object. Then, change original instance. Now I have two different objects.

Reproduce code:
---------------
<?php
class classA {
    public $id = "";
    public function __construct($id) {
        $this->id = $id;
    }
}
class classB {
    public $reference = null;
    public function __construct($ref) {
        $this->reference = $ref;
    }
}
$a = new classA("object 1");
$test = new classB($a);
$a = new classA("object 2");
echo $a->id, "<br>", $test->reference->id;
?>

Expected result:
----------------
object 2
object 2

Actual result:
--------------
object 2
object 1

However, adding references solves the problem:

class classB {
    public $reference = null;
    public function __construct(&$ref) {
        $this->reference =& $ref;
    }
}

This works as expected.

So, the question is are objects being passed by reference or what?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-28 20:42 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-03-01 13:04 UTC] tigr at mail15 dot com
Checked on these snaps, no changes.

Well, maybe this is due to incorrect understanding of what object passing by reference means? Maybe this is a documentation problem?

I've added detailed explanation here:
http://www.php.net/manual/en/language.oop5.basic.php
 [2005-03-01 14:48 UTC] ericvanblokland at gmail dot com
I don't understand your issue, every variable type is passed by value, unless the function definition explicitly says it has to be passed by reference.
 [2005-04-27 15:54 UTC] vrana@php.net
This is already documented (with example showing the difference between = and =&) at http://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.new
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 03:01:32 2024 UTC