Re: Global variable unset in function

From: Date: Wed, 31 May 2000 19:35:05 +0000
Subject: Re: Global variable unset in function
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-755@lists.php.net to get a copy of this message
On Wed, May 31, 2000 at 12:39:53PM -0700, John Hamlik wrote: > Has anyone seen a work around to this Bug 3647. We have the problem as well > where we can't unset a global within a function. It is removed from the > local scope but not the global. > If you look, the bottom of the bug report contains several workarounds. --------------------- Bug 3647 ----------------------- Changing a Global variable in a function fails when the Global variable is unset. An easy example: <?php class test { var $a="a here"; var $b = "b here"; } $t = new test; $t->a = "b here"; function changeGlobal() { GLOBAL $t; unset($t); $t = new test; } echo "t->a = $t->a\n"; changeGlobal(); echo "t->a = $t->a\n"; ?> When executed, this displays: t->a = b here t->a = b here proving that the new assignment to 't' in changeGlobal had no effect. Note that the result is correct if using GLOBALS["t"] in changeGlobal; it also works when removing the 'unset' statement in changeGlobal or redeclaring "GLOBAL $t" right after the unset. Presumably the function forgets the scope of 't' after the unset is executed. ------------------------------------------- Perhaps one of those would work for you? -- Chris Moyer -- cmoyer@chekinc.com -- PHP Developer -- Chek.com

« previous php.general (#755) next »