php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48483 this pointer is not passed by reference correctly
Submitted: 2009-06-06 00:39 UTC Modified: 2009-06-09 02:11 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:3 (100.0%)
From: chase at circleofmoms dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.9 OS: ubuntu 8.04
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: chase at circleofmoms dot com
New email:
PHP Version: OS:

 

 [2009-06-06 00:39 UTC] chase at circleofmoms dot com
Description:
------------
When you pass the $this pointer by reference and change data for the object it points to, that data is not correctly updated.

Reproduce code:
---------------
function changeAddress(&$oldThing) {
    $oldThing = new Thing();
}

class Thing {
  var $data;
  public function updateData($newData) {
    ThingChanger::changeData($this,$newData);
    var_dump($this);
    var_dump($this->data);
  }
}

class ThingChanger {
  public static function changeData(&$thingToChange,$newData) {
    changeAddress($thingToChange);
    $thingToChange->data = $newData;
  }
}

$thing = new Thing();
$thing->data = "foo";
$thing->updateData("bar");

Expected result:
----------------
object(Thing)#2 (1) {
  ["data"]=>
  string(3) "bar"
}
string(3) "bar"


Actual result:
--------------
object(Thing)#2 (1) {
  ["data"]=>
  string(3) "bar"
}
string(3) "foo"


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-09 02:11 UTC] scottmac@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

You can't re-assign $this, ever. It prints an error normally but when references are used it can't be properly detected.

Try the following and see the error.
<?php
class Thing {
  var $data;
  public function updateData($newData) {
	$this = new Thing();
  }
}

$thing = new Thing();
$thing->data = "foo";
$thing->updateData("bar");
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Dec 04 15:00:01 2025 UTC