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 #172698 is a reply to message #172697] Sat, 26 February 2011 14:24 Go to previous messageGo to previous message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma:
Senior Member
sheldonlg wrote:
> 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.
>

That's maybe possible if you are writing OO code, but it is often
simpler when having functions that modify several different variables,
to have then as globals, in order not to either pass pointers to the
functions, or try and work out a way to return more than one variable
from a function.



> 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.
>

I don't do OOP, but it seems to me that a static class variable accessed
by a class method IS in all but name a global variable.

The only difference being that you can somewhat control access via the
method..

I wont be too definite on that point, because if I were writing
something so complex that OOP was needed to keep track of it, for sure I
wouldn't be writing it in PHP :-)

I wont get liked for saying it, but I regard PHP as a simple replacement
for script or BASIC as a noddy way for inexpert programmers to rapidly
concoct web pages with a bit more functionality. If you want to write
large complex projects Real Men Use C++ etc etc ;-)

In short, the OOP aspects of PHP are pure pretension...

(I'll get my coat)
[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 21:40:11 GMT 2024

Total time taken to generate the page: 0.04100 seconds