Req #44552 [Opn]: $this can be overwritten by setting a reference
| From: | johannes@php.net | Date: | Wed, 22 Dec 2010 12:36:48 +0000 |
| Subject: | Req #44552 [Opn]: $this can be overwritten by setting a reference | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-157231@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=44552&edit=1
ID: 44552
Updated by: johannes@php.net
Reported by: andre at webkr dot de
Summary: $this can be overwritten by setting a reference
Status: Open
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
Operating System: Tested on Windows and Linux
PHP Version: 5.2.5
Block user comment: N
Private report: N
New Comment:
We can't prevent people from shooting in their feet. We can make it a
bit harder but not fully prevent it
Previous Comments:
------------------------------------------------------------------------
[2008-03-27 15:38:31] andre at webkr dot de
Description:
------------
Usually $this cannot be set. But by creating a reference to $this it
can.
In the example the reference is created by the =& operator. The problem
is also valid when passing $this to a function by reference.
I think an error should be generated because both $this = 1 and $this =&
$x do generate an error.
Reproduce code:
---------------
class testclass
{
function foo()
{
var_dump($this); // object(testclass)#1 (0) { }
$this->bar();
var_dump($this); // object(testclass)#1 (0) { }
}
function bar()
{
$a =& $this;
$a = 1;
var_dump($this); // int(1)
}
}
$b = new testclass;
$b->foo();
Expected result:
----------------
Fatal error: Cannot re-assign $this in eval()'d code on line 12
Actual result:
--------------
object(testclass)#1 (0) { }
int(1)
object(testclass)#1 (0) { }
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=44552&edit=1