php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36357 $tmp =& $this; is allowed!
Submitted: 2006-02-10 17:05 UTC Modified: 2006-02-13 10:31 UTC
From: dams@php.net Assigned: dmitry (profile)
Status: Not a bug Package: Class/Object related
PHP Version: 5.1.2 OS: Any
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: dams@php.net
New email:
PHP Version: OS:

 

 [2006-02-10 17:05 UTC] dams@php.net
Description:
------------
Using =&, it is possible to modify $this with the constructor. 

This shouldn't be allowed, just like for 
$tmp = $this;

Reproduce code:
---------------
<?php

class B {

  function B() {

  }

}

class A {

  function A() {
    $tmp =& $this;
    var_dump($this); // outputs : object(A)
    $tmp = new B();
    var_dump($this); // outputs : object(B)
  }

}

$a = new A();

var_dump($a); // outputs : object(A)

?>


Expected result:
----------------
object(A)#1 (0) {
}
object(A)#2 (0) {
}
object(A)#1 (0) {
}

// current result : 
object(A)#1 (0) {
}
object(B)#2 (0) {
}
object(A)#1 (0) {
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-02-13 10:31 UTC] dmitry@php.net
$tmp =& $this is proper code (it can be dangerous, but this doesn't mean improper).

Removing support for assignment and passing $this by reference will break a lot of existing PHP scripts.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jan 03 03:01:29 2025 UTC