FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs [message #178792 is a reply to message #178789] Sat, 04 August 2012 17:18 Go to previous messageGo to previous message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma:
Senior Member
Am 03.08.2012 19:45, schrieb J. Frank Parnell:
> <?php
> class test{
> function __construct(){
> $this->makevar();
> global $var;
> echo '<hr>__const(): '.$var;
> }
> function makevar(){
> $var = 'something';
> echo '<hr>just declared in makevar(): '.$var;
> global $var;
> echo '<hr>after global in makevar(): '.$var;
> }
> }//class
>
> $t = new test;
> echo '<hr>outside class: '.$var;
> global $var;
> echo '<hr>outside class after global: '.$var;
>
> ?>
> this outputs:
> just declared in makevar(): something
> after global in makevar(): [nothing]
> __const():[nothing]
> outside class: [nothing]
> outside class after global: [nothing]
>
>
> So, 1, why does global $var in the makevar() make it null?
> and B, why dont I have any access to $var in the __constructor or outside the class?
>

Your code is nonsense, you have to declare a variable as global _before_ using it.

> In the real script, $var will be an instance of a different class that I want to use all over the place.
>

Have a look at the singleton pattern.

/Str.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: str_replace, replace array with string AND array
Next Topic: Excellent new opportunity for developers to monetize more with their Apps.
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 14:35:37 GMT 2024

Total time taken to generate the page: 0.04999 seconds