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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
5 + 31 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2021 The PHP Group
All rights reserved.
Last updated: Sun Dec 05 12:03:38 2021 UTC