From: m dot adeelnawaz at ymail dot com Operating system: Windows 7 / Linux PHP version: 5.5.0 Package: Class/Object related Bug Type: Bug Bug description:$this can be changed using reference
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 bug report at https://bugs.php.net/bug.php?id=65160&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=65160&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=65160&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=65160&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=65160&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=65160&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=65160&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=65160&r=needscript Try newer version: https://bugs.php.net/fix.php?id=65160&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=65160&r=support Expected behavior: https://bugs.php.net/fix.php?id=65160&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=65160&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=65160&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=65160&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65160&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=65160&r=dst IIS Stability: https://bugs.php.net/fix.php?id=65160&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=65160&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=65160&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=65160&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=65160&r=mysqlcfg