php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44552 $this can be overwritten by setting a reference
Submitted: 2008-03-27 15:38 UTC Modified: 2010-12-22 15:04 UTC
From: andre at webkr dot de Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.5 OS: Tested on Windows and Linux
Private report: No CVE-ID: None
 [2008-03-27 15:38 UTC] 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) { }

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-22 13:36 UTC] johannes@php.net
-Package: Feature/Change Request +Package: *General Issues
 [2010-12-22 13:36 UTC] johannes@php.net
We can't prevent people from shooting in their feet. We can make it a bit harder but not fully prevent it
 [2010-12-22 15:04 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-12-22 15:04 UTC] johannes@php.net
.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 09:01:28 2024 UTC