Re: static vs global variable [message #172712 is a reply to message #172694] |
Sun, 27 February 2011 09:36 |
Bernd Schulz
Messages: 10 Registered: September 2010
Karma:
|
Junior Member |
|
|
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...
|
|
|