|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-07-25 18:39 UTC] johannes@php.net
-Status: Open
+Status: Bogus
[2010-07-25 18:39 UTC] johannes@php.net
[2010-07-25 18:50 UTC] tyra3l at gmail dot com
[2010-07-26 10:32 UTC] dagdamor10 at mail dot ru
[2010-07-26 10:51 UTC] tyra3l at gmail dot com
[2010-07-26 11:20 UTC] degeberg@php.net
[2010-07-26 11:30 UTC] tyra3l at gmail dot com
[2012-02-24 15:15 UTC] stelian dot mocanita at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 18:00:01 2025 UTC |
Description: ------------ As some closed bug-reports and the "PHP Fatal error: Cannot re-assign $this" states, the $this should be read-only/inmutable in PHP5. but with some tricks(variable variables mostly), you can walk-around this constraint. See the Test script. I don't know the importance of this restriction, and with reflection you can shoot you in the leg anyway, so maybe this can be left as is. Test script: --------------- <?php error_reporting(E_ALL); $var = new StdClass(); $var->foo = 'bar'; //$this = $var; // PHP Fatal error: Cannot re-assign $this $GLOBALS['this'] = $var; var_dump($this); $var->foo = 'baz'; $foo = 'this'; $$foo = $var; var_dump($this); foo($this); function foo($this){ //global $this; // PHP Fatal error: Cannot re-assign $this // $this = $GLOBALS['var']; // PHP Fatal error: Cannot re-assign $this var_dump($this); $GLOBALS['this']->foo = 'baw'; $$GLOBALS['foo'] = $GLOBALS['this']; var_dump($this); } Expected result: ---------------- PHP Fatal error: Cannot re-assign $this for every attempt to overwrite $this Actual result: -------------- you can set $this in the global scope through $GLOBALS, with argument in functions, and with variable variables in everywhere.