Re: Variable Scope Problem

From: Date: Fri, 23 May 2003 02:17:53 +0000
Subject: Re: Variable Scope Problem
Groups: php.gtk.general 
Request: Send a blank email to php-gtk-general+get-3470@lists.php.net to get a copy of this message
You don't give enough information to solve the problem, but I have my suspicions. Remember that when you "globalize" a variable, you are in fact creating a reference to the globalized variable. You don't actually have the variable in your local scope. These two functions are equivalent: Function foo() { global $bar; echo $bar; } Function foo() { $bar=&$GLOBALS[bar]; echo $bar; } If you unset() a globalized variable, you delete the reference to the variable, not the variable itself. So, this WON'T work like it apparently should: $var='foo'; function bar() { global $var; echo $var; unset($var); } bar(); echo $var; In this case, you'd see 'foo' two times. For more details, and too many posts to mention on the subject of variable scope, see http://us2.php.net/manual/it/language.variables.scope.php My solution would be to have a global memory variable called $windows, and then refer to sub-elements, so that your function becomes: function IncomingMsgHandler($message, $from) { $IMWindowname = "IMWindow".$from; global $windows; if(!$windows[$IMWindowname]) // THIS SHOULD WORK { NewIMWindow($from); } // ...update window text... } This would then allow you to have a function like Function CloseAllWindows() { global $windows; reset($windows); while (list($key, $window)=each($windows)) if (is_object($window)) $window->Destroy(); } -Ben On Thursday 22 May 2003 04:12 pm, kyle@ktyp.com wrote: > I'm working on a php-gtk Jabber client called Jabble and I've been having > trouble trying to create and update new message windows. Here is my problem > (copied from project's website): > > Everytime Jabble gets a message from a new client it creates a new message > window. The name of said window is $IMWindow_usernameAThost and is created > in a "NewIMWindow()" function. So if kyle@localhost messages you, a new > window called $IMWindow_kyleATlocalhost will be created. No problem so far. > > Now kyle@localhost decides to send you another message. Well when Jabble > calls its function to update window text, is checks to see if > $IMWindow_kyleATlocalhost exists. So far it is always nonexistant. > It looks like this: > > > function NewIMWindow($from) > { > $IMWindowname = "IMWindow".$from; > global $$IMWindowname; // Variable Variable > if(!$$IMWindowname) // if window doesn't exist > { > ...create new window... > } > } > > function IncomingMsgHandler($message, $from) > { > $IMWindowname = "IMWindow".$from; > global $$IMWindowname; > if(!$$IMWindowname) // THIS IS ALWAYS TRUE > { > NewIMWindow($from); > } > ...update window text... > } > > The problem is when the window variable is created in NewIMWindow other > functions can't seem to access it even after declaring "global". > > - Kyle (kyle@ktyp.com) -------------------------------------------------------

« previous php.gtk.general (#3470) next »