php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31884 setting reference to $this in class method
Submitted: 2005-02-08 14:47 UTC Modified: 2005-02-09 00:00 UTC
From: camka at email dot ee Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.0.3 OS: Linux
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: camka at email dot ee
New email:
PHP Version: OS:

 

 [2005-02-08 14:47 UTC] camka at email dot ee
Description:
------------
When setting reference to $this object, using "=&" operator in a class method doesn't seem to work as expected (or my understanding is completely wrong). When unsetting object outside of the class methods - link breaks and won't unset the referenced objects.

Reproduce code:
---------------
<?php 
class uu 
{ 
    public $ref; 

    public function set_me_as_ref(uu &$whom) 
    { 
        $whom->ref = &$this; 
    } 
} 

$obj1 = new uu(); 
$obj2 = new uu(); 

$obj2->set_me_as_ref($obj1); 

$obj2 = null; // this won't unset $obj1->ref, but unsets only $obj2 object 
// $obj1->ref = null; // this won't unset the $obj2 object, but unsets only $obj1->ref 

echo '<pre>'; 
var_dump($obj1, $obj2); 
?> 

Expected result:
----------------
$obj1->ref is supposed to be null
$obje2 - is supposed to be null

Actual result:
--------------
$obj1->ref => living object
$obj2 => null

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-08 16:17 UTC] pollita@php.net
$this is a special variable and is processed differently than other variables.

Creating references to it simply won't work the way you're expecting it to.
 [2005-02-09 00:00 UTC] camka at email dot ee
Could you please point any documentation where such a behaviour is described more exactly.

thank you in advance, and sorry, in case I missed this in the manual.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 13:01:33 2025 UTC