note 88570 added to language.variables.scope

From: Date: Thu, 29 Jan 2009 15:17:51 +0000
Subject: note 88570 added to language.variables.scope
Groups: php.notes 
Request: Send a blank email to php-notes+get-149945@lists.php.net to get a copy of this message
in response to the issue ddarjany at yahoo dot com raised: Just to clarify; the same goes for classes. <?php class global_reference { public $val; public function __construct () { global $var; $this->val = $var; } public function dump_it () { debug_zval_dump($this->val); } public function type_cast () { $this->val = (int) $this->val; } } $var = "x"; $obj = new global_reference(); $obj->dump_it(); $obj->type_cast(); echo "after change "; $obj->dump_it(); echo "original $var\n"; ?> The work-around is of course changing the assignment in the constructor to a reference assignment as such: <?php //.... $this->val = &var; //.... ?> ---- Server IP: 117.55.229.1 Probable Submitter: 121.222.234.117 ---- Manual Page -- http://www.php.net/manual/en/language.variables.scope.php Edit -- https://master.php.net/note/edit/88570 Del: integrated -- https://master.php.net/note/delete/88570/integrated Del: useless -- https://master.php.net/note/delete/88570/useless Del: bad code -- https://master.php.net/note/delete/88570/bad+code Del: spam -- https://master.php.net/note/delete/88570/spam Del: non-english -- https://master.php.net/note/delete/88570/non-english Del: in docs -- https://master.php.net/note/delete/88570/in+docs Del: other reasons-- https://master.php.net/note/delete/88570 Reject -- https://master.php.net/note/reject/88570 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#149945) next »