Re: Class and variable scope

From: Date: Mon, 19 May 2003 18:59:16 +0000
Subject: Re: Class and variable scope
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-148194@lists.php.net to get a copy of this message
On Mon, 19 May 2003 14:16:55 -0400, you wrote: >Im declaring a variable in a class constructor, and then including >another file which depends on this declared variable. >In this included file, the variable behaves as if it wasn't even set. >I've provided some mock code to explain what Im using. >If you need more info, feel free to ask. Thanks. > >Class file >----- ><?php >class Class >{ > function Class() > { > $foo = 'some-value'; > include('./bar.php'); > } >} >?> > >bar.php >------- ><?php >global $foo; >if (!isset($foo)) >{ > $foo = 'some-default-value'; >} >var_dump($foo); // <- Shouldn't $foo be 'some-value' instead of >'some-default-value' here? >?> Try removing the global statement. Think of the include() as a cut'n'paste into the body of your constructor... after inclusion the bar.php code is in the same function as the variable declaration, so it doesn't need it's scope messed with. (As to why the global statement is behaving as it is... not a clue).

« previous php.general (#148194) next »