Re: static vs global variable [message #172697 is a reply to message #172694] |
Sat, 26 February 2011 13:13 |
sheldonlg
Messages: 166 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 2/25/2011 7:40 PM, tobycraftse(at)yahoo(dot)com wrote:
>
> 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
The only globals I would EVER use are the super-globals such as $_SESSION.
So far the only real use I have found for a static class variable is in
setting it once when the class instance is first created. Then, other
invocations would be via a ClassName::getInstance() to return the
already created instance of the class by testing for that variable not
being NULL.
Any other variable that is needed that from that class would be obtained
from a mutator method such as getThisVariable() which would return the
class variable $thisVariable.
So, there is really only one static class variable and the rest are
ordinary class variables with methods provided for their access from the
outside once the instance is retrieved.
--
Shelly
|
|
|