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 #172706 is a reply to message #172700] Sat, 26 February 2011 19:12 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/26/2011 9:24 AM, The Natural Philosopher wrote:
>> 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
>
> Obviously you don't.
>
>> by a class method IS in all but name a global variable.
>
> Not at all. Consider, for example, having a configuration file written,
> say, in XML. Then you could have a Configuration.class.php file that
> reads that configuration file and stores the parameters in class
> variables. None of those are static, but are unique to the particular
> instance of Configuration.class.php.
>
> Now many places in the code you may need one or more of those values.
> That is where it becomes useful to grab the instance already created,
> which has those values already, than to have to reread the configuration
> file. To do that there is a static variable in the class which was set
> at the time of the first instantiation. A call to a method
>
> $cfg = Configuration::getInstance();
>
> returns that instance and
>
> $cfg->getThePropertyYouWant();
>
> returns that property.
>
> The problem with global variables is that you don't know if somewhere
> else in the code you have inadvertently changed it. Here the data are
> all contained within an instance of the class. The only way anything
> inside is changes is by specific action of mutator set methods. Access
> is controlled by the mutator get methods. All that is done in _this_
> case to retrieve and already created instance of the class, rather than
> recreating it each time -- and that is done by a specific method as
> well, the getInstance().
>
>>
>> The only difference being that you can somewhat control access via the
>> method..
>
> The only difference between breathing and not breathing is being alive
> or dead.
>
No, that isn't fair.
A simple acess to a global variable is no different from global adcess
to some function that alters its value in terms of SCOPE. You may of
course limit the *operations* on that variable, but you are not really
de-scoping it.

In short its just as easy to make a buggers muddle in OOP, it jus5
happens with different syntax.


>>
>> 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 :-)
>
> Your loss. PHP5 has inheritance, abstact classes, implements, and all
> those other nice OO features.

I don't use a 30 ton press to put a nail in a wall either.

OOP has its place but there is no need to fall in loive with it and get
religion. Its a good solution to a certain class of problems, but its
neither THE solution not yet a GOOD solution in all cases.

My pint being that anine using an interepreted language to construct a
huge data crunching or screen handling edifice is probably going to be
disappointed performance wise anyway, so dont use 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 ;-)
>
> What do you have in C++ that you don't have in PHP other than -- UGH! --
> multiple inheritance?

Speed. The ability to construct libraries that are dynamically linked.
The ability to insert assembler coded sections for critical speed
tuning. Source code security. ..I am sure many others can think of more..


You would be floored to see some of the stuff I
> have written and seen in PHP.

I have no doubt I would,. You would be floored to see some of what i
have written on assembler. The real question is, whether you (or I)
would have been better off writing it in something else.




> Also, I think Java is much better than
> C++ and PHP5 is very much like Java in many ways.
>
More interpretation, and more unqualified support for a language with
no reference to context.

Do you think Java or PHP is better than say C for a microcontroller?

Or as the base language for an operating system?

Do you think the Mysql core should be re written in Java?


>>
>> In short, the OOP aspects of PHP are pure pretension...
>
> The more you post like this, the more I will have to side with Jerry.
>

Well your lack of humour at this point puts you somewhat towards his
orientation.

>>
>> (I'll get my coat)
>
> Good idea.
>

Lok I love PHP as a simple fast way to get web pages that have
intelligence and talk to an SQL backend. But the be all and end all of
programming languages?

Pull the other one.
[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: Wed Nov 27 02:22:09 GMT 2024

Total time taken to generate the page: 0.04675 seconds