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

 ID:                 65160
 Updated by:         a...@php.net
 Reported by:        m dot adeelnawaz at ymail dot com
 Summary:            $this can be changed using reference
 Status:             Open
 Type:               Bug
 Package:            Class/Object related
 Operating System:   Windows 7 / Linux
 PHP Version:        5.5.0
 Block user comment: N
 Private report:     N

 New Comment:

looks like a real mess, namely here's a modified snippet

<?php

class a{}
class b{
        function test(){
                $a = &$this;

                echo get_class($this).PHP_EOL;
                echo get_class($a).PHP_EOL;

                $a = new a();

                echo get_class($this).PHP_EOL;
                echo get_class($a).PHP_EOL;
        }

        function test2()
        {
                echo get_class($this).PHP_EOL;
        }
        function test3()
        {
                $a = &$this;

                $a = new a;

                echo get_class($this).PHP_EOL;
                echo get_class($a).PHP_EOL;

                $this->test();
        }
}

$b = new b();
$b->test();
$b->test2();
$b->test3();

$a = new a;
$a->test();

especially b:test3() - it reports both $a and $this having class 'a', but $this-
>test() works after that. Whereby (new a)->test() will die on undefined method.


Previous Comments:
------------------------------------------------------------------------
[2013-06-28 14:59:20] m dot adeelnawaz at ymail dot com

Actual result:
--------------
b
b
a
a

------------------------------------------------------------------------
[2013-06-28 14:45:15] m dot adeelnawaz at ymail dot com

Description:
------------
In an object method, $this must always be a reference to the caller object.
Now $this can not be re-assigned but re-assigning is not the only way to modify 
$this. The code below demonstrates another way to set $this to something other 
than the caller object.

This is what happens. $this is a reference (or copy of the identifier) to the 
caller object. After doing this
        $a = &$this;
$a and $this are both pointing to the same reference to the object identifier. 
So changing one of them (assigning them to something not by reference) will 
change the reference for both of them.
After executing this line
        $a = new a();
Both $a and $this start pointing to the reference of the identifier to the 
newly created object. You can change $this's reference by setting $a (not by 
reference) to any non-null variable or value.

Test script:
---------------
class a{}
class b{
        function test(){
                $a = &$this;
                
                echo get_class($this).'<br/>';
                echo get_class($a).'<br/>';
                
                $a = new a();
                
                echo get_class($this).'<br/>';
                echo get_class($a).'<br/>';
        }
}

$b = new b();
$b->test();

Expected result:
----------------
One of the following should happen when I run the code above.

1. The code should produce a fatal error on line 3 ($a = &$this;) saying that 
referencing to $this is not allowed.

2. The code should allow me to create a reference to $this in $a but assigning 
any non-null value to $a (not by reference) should produce a fatal error saying 
that $a is a reference to $this so it should (first) be assigned by reference 
to some variable / object other than current object.

3. The code should allow me to create a reference to $this in $a but then the 
same rules should apply to $a as $this with one exception that $a can be 
unset().

Actual result:
--------------
b
b
b
a


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=65160&edit=1

Reply via email to