php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47080 Type of variable changes after it has been returned by reference
Submitted: 2009-01-12 14:20 UTC Modified: 2009-02-10 10:57 UTC
From: foddex at foddex dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.8 OS: Linux Fedora 9
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: foddex at foddex dot net
New email:
PHP Version: OS:

 

 [2009-01-12 14:20 UTC] foddex at foddex dot net
Description:
------------
When a class member variable gets returned by reference, the member variable itself turns into a reference. See the URL below for more detailed information (it contains a lot of comments).

The basic idea is:
class A { 
  var $foo; 
  function &get() { return $this->foo; }
}

If we clone an instance of A when get() hasn't been returned yet, the value of $foo gets cloned. If we call get(), and then clone the instance of A, the cloned object references the same data as the original! This might be logical because it had become a reference, but this behavior is not documented and thus pretty unexpected, IMHO.

Reproduce code:
---------------
http://pastebin.be/16008

Expected result:
----------------
object(A)#1 (1) {
  ["foo"]=>
  int(42)
}
int(42)
int(43)
int(42)
object(A)#1 (1) {
  ["foo"]=>
  int(43)
}


Actual result:
--------------
object(A)#1 (1) {
  ["foo"]=>
  int(42)
}
int(42)
int(43)
int(43)
object(A)#1 (1) {
  ["foo"]=>
  &int(43)
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-02-10 10:57 UTC] tony2001@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

"A reference" means several variables share the same value.
http://www.php.net/manual/en/language.references.whatare.php
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 18:01:33 2025 UTC