Edit report at http://bugs.php.net/bug.php?id=44552&edit=1

 ID:                 44552
 Updated by:         johan...@php.net
 Reported by:        andre at webkr dot de
 Summary:            $this can be overwritten by setting a reference
-Status:             Open
+Status:             Bogus
 Type:               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:

.


Previous Comments:
------------------------------------------------------------------------
[2010-12-22 13:36:48] johan...@php.net

We can't prevent people from shooting in their feet. We can make it a
bit harder but not fully prevent it

------------------------------------------------------------------------
[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

Reply via email to