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

Home » Imported messages » comp.lang.php » static vs global variable
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: static vs global variable [message #172722 is a reply to message #172712] Sun, 27 February 2011 16:13 Go to previous message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma:
Senior Member
On 2/27/2011 4:36 AM, Bernd Schulz wrote:
> Am 26.02.2011 01:40, schrieb tobycraftse(at)yahoo(dot)com:
>>
>> if I have a couple of variable want to included by many php file
>>
>> should i use global or static class variable?
>>
>> global is kinda trouble as i need to delcare global every php file i
>> want to use it.
>>
>> static variable do not need to declare up front
>
> Why not using a singleton class?
>
> Short example:
> class MYVAR
> {
> function getInstance()
> {
> static myobj = false;
> if (myobj === false)
> myobj = new MYVAR;
> return myobj;
> }
> }
>
> Using it:
> $x = MYVAR::getInstance();
> $x->global = 7;
>
> etc...

You are a bit late to the table. I already said this about four posts
ago and TNP keeps objecting. (By the way, you left out the public
function setGlobal($value) {
$this->global = $value;
}

and the declaration of
private $global;
as the class variable.

You would then have
$x->setGobal(7);


--
Shelly
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: ODBC Connection
Goto Forum:
  

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

Current Time: Sat Nov 23 07:40:28 GMT 2024

Total time taken to generate the page: 0.05069 seconds