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
Switch to threaded view of this topic Create a new topic Submit Reply
static vs global variable [message #172694] Sat, 26 February 2011 00:40 Go to next message
tobycraftse is currently offline  tobycraftse
Messages: 1
Registered: February 2011
Karma: 0
Junior Member
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
Re: static vs global variable [message #172695 is a reply to message #172694] Sat, 26 February 2011 02:14 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
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

If it's something you're using in functions, you should pass the values
as parameters to the functions. Otherwise your function code is
needlessly dependent on external values.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: static vs global variable [message #172697 is a reply to message #172694] Sat, 26 February 2011 13:13 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
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
Re: static vs global variable [message #172698 is a reply to message #172697] Sat, 26 February 2011 14:24 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
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)
Re: static vs global variable [message #172700 is a reply to message #172698] Sat, 26 February 2011 15:02 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
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.

>
> 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 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? You would be floored to see some of the stuff I
have written and seen in PHP. Also, I think Java is much better than
C++ and PHP5 is very much like Java in many ways.

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

>
> (I'll get my coat)

Good idea.

--
Shelly
Re: static vs global variable [message #172704 is a reply to message #172700] Sat, 26 February 2011 16:48 Go to previous messageGo to next message
Mladen Gogala is currently offline  Mladen Gogala
Messages: 13
Registered: December 2010
Karma: 0
Junior Member
On Sat, 26 Feb 2011 10:02:29 -0500, sheldonlg wrote:

> What do you have in C++ that you don't have in PHP other than -- UGH! --
> multiple inheritance? You would be floored to see some of the stuff I
> have written and seen in PHP. Also, I think Java is much better than
> C++ and PHP5 is very much like Java in many ways.

I've been following this group quite a while and I haven't seen anybody
mentioning PHAR, which is a noteworthy addition to PHP. One thing that is
missing, though, is creating something like the CLASSPATH variable, which
would make it possible for a PHP script to search a PHAR archive
automatically, without having to include phar://archive.phar/file.php
things. I agree with you completely, there are numerous similarities
between Java and PHP5.



--
http://mgogala.byethost5.com
Re: static vs global variable [message #172706 is a reply to message #172700] Sat, 26 February 2011 19:12 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
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.
Re: static vs global variable [message #172708 is a reply to message #172706] Sat, 26 February 2011 19:49 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
On 2/26/2011 2:12 PM, The Natural Philosopher wrote:
> 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?

Who ever said _that_? You want to write microprocessor code? Of course
it not PHP. But, TNP, this a PHP news group. We use PHP for what it is
designed -- and for that it is fantastic.

--
Shelly
Re: static vs global variable [message #172709 is a reply to message #172698] Sun, 27 February 2011 01:42 Go to previous messageGo to next message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma: 0
Member
"The Natural Philosopher" <tnp(at)invalid(dot)invalid> escribió en el mensaje
news:ikb2fq$o8f$1(at)news(dot)albasani(dot)net...
>
> 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.

Hi,

Perhaps I have become thick with the years, but I can't imagine why a
function should be required to modify several different global
variables.

If you don't want to enter into all the details and subtleties of OOP,
fine. But using and object to pack all the data required by a given
process, and pass it to any function that might need it is even easier
than having to remember the names of every global variable involved in
a complex procedure.

I am completely new to PHP, but it is universal programming common
sense, like using structs in C, or Records in Pascal.

--
Re: static vs global variable [message #172710 is a reply to message #172709] Sun, 27 February 2011 02:38 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/26/2011 8:42 PM, Leonardo Azpurua wrote:
>
> "The Natural Philosopher" <tnp(at)invalid(dot)invalid> escribió en el mensaje
> news:ikb2fq$o8f$1(at)news(dot)albasani(dot)net...
>>
>> 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.
>
> Hi,
>
> Perhaps I have become thick with the years, but I can't imagine why a
> function should be required to modify several different global variables.
>
> If you don't want to enter into all the details and subtleties of OOP,
> fine. But using and object to pack all the data required by a given
> process, and pass it to any function that might need it is even easier
> than having to remember the names of every global variable involved in a
> complex procedure.
>
> I am completely new to PHP, but it is universal programming common
> sense, like using structs in C, or Records in Pascal.
>
> --
>

Leonardo, as you will learn if you stick around here long enough, TNP is
not a programmer (nor the electrical engineer he claims to be). He is
merely a hacker who thinks he knows everything - but knows very little
about good programming practices. He makes all kinds of claims as to
what he's done in the past, but his comments in this newsgroup have
repeatedly shown the opposite.

It's why he uses a pseudonym instead of his real name - he doesn't want
anyone to find out who he really is.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: static vs global variable [message #172711 is a reply to message #172708] Sun, 27 February 2011 09:01 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
sheldonlg wrote:
> On 2/26/2011 2:12 PM, The Natural Philosopher wrote:
>> 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?
>
> Who ever said _that_? You want to write microprocessor code? Of course
> it not PHP. But, TNP, this a PHP news group. We use PHP for what it is
> designed -- and for that it is fantastic.
>
That was my point.

Is 50 pages of complex OOP code what it was designed for?
Re: static vs global variable [message #172712 is a reply to message #172694] Sun, 27 February 2011 09:36 Go to previous messageGo to next message
Bernd Schulz is currently offline  Bernd Schulz
Messages: 10
Registered: September 2010
Karma: 0
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...
Re: static vs global variable [message #172713 is a reply to message #172709] Sun, 27 February 2011 09:55 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Leonardo Azpurua wrote:
>
> "The Natural Philosopher" <tnp(at)invalid(dot)invalid> escribió en el mensaje
> news:ikb2fq$o8f$1(at)news(dot)albasani(dot)net...
>>
>> 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.
>
> Hi,
>
> Perhaps I have become thick with the years, but I can't imagine why a
> function should be required to modify several different global variables.
>

Well a simply noddy example is say a program that does a terminal
emulator. The cursor has X,Y coorodintes. Various subroutines are called
to handle keyboard events. They need to modify X AND Y.

There are ways around the 'single return value' that dont use simple
global variables to store the values, but my point is, that its actually
easier and more natural to use them.



> If you don't want to enter into all the details and subtleties of OOP,
> fine. But using and object to pack all the data required by a given
> process, and pass it to any function that might need it is even easier
> than having to remember the names of every global variable involved in a
> complex procedure.
>

It seems little different to me to have to remember a set of class
methods than a set of variable names. If an entity is unique at some
level it needs a unique tag, and you are stuck with having to remember
it, whatever.

> I am completely new to PHP, but it is universal programming common
> sense, like using structs in C, or Records in Pascal.

Well structs/records are a way to package and pass related bits of data,
sure. And things got a lot easier when the compilers I used could return
a struct, and not just a simple integer float or pointer. PHP has its
associative arrays, that perform similar duties.

Still no NEED for OOP.

OOP is supposed to be (as far as a couple of weeks reading it up years
ago suggests), about keeping data and the means to alter it grouped
together and tightly defined BUT....if the methods for altering it are
general enough to be useful, you still can make a mess of the code when
you call the class methods from somewhere else..and it puts a greater
burden on the coders to specify the class interfaces before coding.

Is rather like a database as an 'object' you had better be pretty
rigorous in your table and relationship designs before you start writing
forms to access it.

And if something turns up you didn't think of, you can be in for huge
redesigns if the 'object' has to change to accommodate them.

I fully accept that in a database, this methodology is the least nasty
of all possible approaches. But I wouldn't write a database in PHP.

I suppose my beef is that various programming methodologies have been
developed over the years by people seeking to solve certain problems of
complexity in what they consider more structured and formal ways, and
extend from being 'useful here' to being 'de rigeur everywhere'.

The tool becomes the Rule. 'Gotos should be banned' 'No global
variables' 'Real Men write OOP' etc.

The very worst project I ever worked on, was one where the actual coding
specification had been drawn up from a specification done in Z notation,
as presumably a fashion statement. Months behind, full of programmers
achieving nothing, I finally settled down behind a pint with a fellow
incumbent and asked 'is it just me, or is it true, that say you I and
Fred over there, could throw this lot in the bin and write a system that
worked in three months?' And he said 'true, but it pays well dunnit?'.

In this case the use of some particular formal approach had been dreamed
up by some idiot as a a substitute for actually thinking the problem
through and understanding what the code was supposed to do. I suppose he
had learned it at CompSci, and thought that the methodology was the
problem solving agency, not the analyst.

I am just the lone voice in the wilderness crying out tat befrer you
decide what methods and rules you adopt, sitting down with a sharp
pencil and a sheet of paper and sketching out the nature of the problem
till you understand exactly how you can split it into bits that you can
write clear specifications for you, or someone else, to write code from,
is the most important step. IF your understanding is good, and the
discipline is there, your code will work.

I think it was many years after I had learned to do this that someone
told me what I had been doing was 'structured programming functional
decomposition'. We just called it 'chunking'. :-)

Now if those 'chunks' all seem to map well onto 'objects' all well and
good. Maybe an OOP works. If they map better to procedural subroutines,
use those instead. If what you have is many and various ways of altering
a very very few master parameters, save yourself a lot of overhead and
make those globals.

Tools, not Rules. Intelligent analysis, not slavish adherence to one
formal methodology or language.

Sledgehammers and nuts.
Re: static vs global variable [message #172714 is a reply to message #172712] Sun, 27 February 2011 10:04 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
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...
>
>
>
And what differentiates that from:

global $my_dangerous_global_x;
$my_dangerous_global_x=7;

??

Apart from the fact that the latter is a lot less lines overall. And
probably executes faster.

You HAVE declared a global variable in philosophical terms. You have
just made it lexically complicated (and in machine cycles, longer) to
access it.

Imagine trying to run the batsman out, but being REQUIRED to only throw
the ball to the wicket keeper, as he is the only man allowed to throw it
at the stumps..

All you do is shift your global object (the stumps) from being a simple
target, to the wicket keeper being the global object and the only way to
access the stumps.

I can see the point if you are playing cricket with 17 balls at once,
and you cant have two balls hitting it simultaneously, but just to play
the simple game? Pshaw!. Its madness!
Re: static vs global variable [message #172717 is a reply to message #172711] Sun, 27 February 2011 13:11 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/27/2011 4:01 AM, The Natural Philosopher wrote:
> sheldonlg wrote:
>> On 2/26/2011 2:12 PM, The Natural Philosopher wrote:
>>> 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?
>>
>> Who ever said _that_? You want to write microprocessor code? Of course
>> it not PHP. But, TNP, this a PHP news group. We use PHP for what it is
>> designed -- and for that it is fantastic.
>>
> That was my point.
>
> Is 50 pages of complex OOP code what it was designed for?

If you have t0 pages of complex code, you NEED to be using OOP!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: static vs global variable [message #172718 is a reply to message #172713] Sun, 27 February 2011 14:56 Go to previous messageGo to next message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma: 0
Member
"The Natural Philosopher" <tnp(at)invalid(dot)invalid> escribió en el mensaje
news:ikd71r$a2b$1(at)news(dot)albasani(dot)net...

> Well a simply noddy example is say a program that does a terminal
> emulator. The cursor has X,Y coorodintes. Various subroutines are
> called to handle keyboard events. They need to modify X AND Y.

Well, X and Y do not qualify as "several". And in that particular case
having a Cursor class would be extremely helpful: in your code you
write "cursor->goto($x, $y);" and the cursor goes to that point
"magically". If later have to write an emulator for other terminal,
all you have to do is change the definition of the Cursor class
instead of browsing through all the code.

> There are ways around the 'single return value' that dont use simple
> global variables to store the values, but my point is, that its
> actually easier and more natural to use them.

> It seems little different to me to have to remember a set of class
> methods than a set of variable names. If an entity is unique at some
> level it needs a unique tag, and you are stuck with having to
> remember it, whatever.

IDEs do help a lot in this respect. Sadly, I still haven't found an
IDE for PHP, but in most of them, once you write the name of an object
followed by a dot (I guess it would be by -> in PHP) you get a nice
little window with all of the object public members. So, it is easier
to remember the name of a set of "packers" than the names of
everything packed inside them.

> Well structs/records are a way to package and pass related bits of
> data, sure. And things got a lot easier when the compilers I used
> could return a struct, and not just a simple integer float or
> pointer. PHP has its associative arrays, that perform similar
> duties.

It alsa has Objects, that do the same. Nobody says that an object
_must_ contain code.

> Still no NEED for OOP.
>
> OOP is supposed to be (as far as a couple of weeks reading it up
> years ago suggests), about keeping data and the means to alter it
> grouped together and tightly defined BUT....if the methods for
> altering it are general enough to be useful, you still can make a
> mess of the code when you call the class methods from somewhere
> else..and it puts a greater burden on the coders to specify the
> class interfaces before coding.

If you have to call the code from somewhere else, then it doesn't
belong into that particular object, and packig it there is a design
error that will force you to do nasty things. The right action in that
situation would be to move the general code somewhere else.

> The tool becomes the Rule. 'Gotos should be banned' 'No global
> variables' 'Real Men write OOP' etc.

It has nothing to do with dogmas.

Unrestricted usage of gotos may lead to excesively complex code. OTOH,
a goto jumping outside a deeply nested structure allows for greater
simplification. The actual rule is not that gotos should be banned,
but that simplicity must be sought.

One of the nastier bugs I ever had to solve was because of a global
variable being modified at some point deep inside a chain of calls,
destroying the original value at the calling point. And there was no
need for the variable being global in either of the two points. Ih I
had been particularly careful, the bug wouldn't have occured. But if I
had avoided the use of global variables, unless they were strictly
needed, the need for extra care wouldn't have existed, neither the
bug.

I always tought that real programmers only wrote assembly code using
EDLIN.

> The very worst project I ever worked on, was one where the actual
> coding specification had been drawn up from a specification done in
> Z notation, as presumably a fashion statement. Months behind, full
> of programmers achieving nothing, I finally settled down behind a
> pint with a fellow incumbent and asked 'is it just me, or is it
> true, that say you I and Fred over there, could throw this lot in
> the bin and write a system that worked in three months?' And he said
> 'true, but it pays well dunnit?'.

Managers, specially when they come from a Management School and not
from the field, can be pretty dumb.

OTOH, I can't imagine how to keep a group of programmers in sync
without a lot of formalization. But I have never needed to manage a
large team, nor been part of any, except for my first years as a
programmer, when I was given a HIPO spec, coded the routine, validated
it with a test program that came as part of the specification, had it
punched and handed the cards pack upwards when it was ready.

Anyway, there's a spanish motto that says that "everyone descends the
stairs how they best like".

--
Re: static vs global variable [message #172721 is a reply to message #172711] Sun, 27 February 2011 16:09 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
On 2/27/2011 4:01 AM, The Natural Philosopher wrote:
> sheldonlg wrote:
>> On 2/26/2011 2:12 PM, The Natural Philosopher wrote:
>>> 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?
>>
>> Who ever said _that_? You want to write microprocessor code? Of course
>> it not PHP. But, TNP, this a PHP news group. We use PHP for what it is
>> designed -- and for that it is fantastic.
>>
> That was my point.
>
> Is 50 pages of complex OOP code what it was designed for?

Could be. I have written an accounting application for an intranet
which was sufficiently complex, with enough options, that it was greater
than 50 pages of OOP code. If it were not written in OOP it would
likely have been much more prone to error and a lot harder to develop.

As a famous saying goes "Size doesn't matter. It's what you do with it
that counts."

--
Shelly
Re: static vs global variable [message #172722 is a reply to message #172712] Sun, 27 February 2011 16:13 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
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
Re: static vs global variable [message #172723 is a reply to message #172718] Sun, 27 February 2011 16:17 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
On 2/27/2011 9:56 AM, Leonardo Azpurua wrote:
> Sadly, I still haven't found an IDE for PHP

Look at Eclipse. I use that one for where I have to integrate with CVS
and I use Dreamweaver for other stuff. Both are very good, but I think
Eclipse is more comprehensive.

--
Shelly
Re: static vs global variable [message #172724 is a reply to message #172717] Sun, 27 February 2011 16:20 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
On 2/27/2011 8:11 AM, Jerry Stuckle wrote:
> On 2/27/2011 4:01 AM, The Natural Philosopher wrote:
>> sheldonlg wrote:
>>> On 2/26/2011 2:12 PM, The Natural Philosopher wrote:
>>>> 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?
>>>
>>> Who ever said _that_? You want to write microprocessor code? Of course
>>> it not PHP. But, TNP, this a PHP news group. We use PHP for what it is
>>> designed -- and for that it is fantastic.
>>>
>> That was my point.
>>
>> Is 50 pages of complex OOP code what it was designed for?
>
> If you have t0 pages of complex code, you NEED to be using OOP!

I fully agree. Having been there, it was MUCH easier to keep a solid
train of thought with OOP.

--
Shelly
Re: static vs global variable [message #172762 is a reply to message #172718] Mon, 28 February 2011 10:02 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Leonardo Azpurua wrote:
>
>
> OTOH, I can't imagine how to keep a group of programmers in sync without
> a lot of formalization. But I have never needed to manage a large team,
> nor been part of any, except for my first years as a programmer, when I
> was given a HIPO spec, coded the routine, validated it with a test
> program that came as part of the specification, had it punched and
> handed the cards pack upwards when it was ready.
>
In the project I mentioned, we realised that 3-4 good people could have
done a better job than all the formalized teamwork and something like
60 coders.

Once again, management is a discipline where too many idiots who have
taken courses in it manage to fail to understand what lies behind the
courses they took.

And apply formal stupidity wit a ruthlessness only the truly ignorant
can possess.

Once you get into formal madness, you WILL need it to control the huge
teams you find you need just to keep up with all the paperwork.
Re: static vs global variable [message #172763 is a reply to message #172724] Mon, 28 February 2011 10:14 Go to previous message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
sheldonlg wrote:
> On 2/27/2011 8:11 AM, Jerry Stuckle wrote:
>> On 2/27/2011 4:01 AM, The Natural Philosopher wrote:
>>> sheldonlg wrote:
>>>> On 2/26/2011 2:12 PM, The Natural Philosopher wrote:
>>>> > 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?
>>>>
>>>> Who ever said _that_? You want to write microprocessor code? Of course
>>>> it not PHP. But, TNP, this a PHP news group. We use PHP for what it is
>>>> designed -- and for that it is fantastic.
>>>>
>>> That was my point.
>>>
>>> Is 50 pages of complex OOP code what it was designed for?
>>
>> If you have t0 pages of complex code, you NEED to be using OOP!
>
> I fully agree. Having been there, it was MUCH easier to keep a solid
> train of thought with OOP.
>

I would say of you have 50 pages of complex code what you need is
documentation and some overall diagram of what you are trying to achieve
that clearly outlines how bits are hung together. It might be a state
diagram, it might be a flow chart, or it might be a suite of object
specifications.


I would certainly never rely on the code itself to be self documenting.

If it were assembler, and I have written far larger code examples than
that - Assembler is hugely verbose - and it for example contained global
variables, then one necessary bit of documentation would be to insist
that anyone who writes a routine that changes that global variable add a
line 'modified by routine 'blah' written by 'coder X' in it.

Its a habit I still adopt to an extent. Even terminating curly braces
tend to have } //endif ( key alpha pressed) inserted even though the
editor I use is fully capable of collapsing each and every block.

Again the point is this, language features are no substitute for proper
documentation and clear thinking and correct chunking. And they are just
as likely to obfuscate rather than clarify if used slavishly and
inappropriately.
  Switch to threaded view of this topic Create a new topic Submit Reply
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 22:30:54 GMT 2024

Total time taken to generate the page: 0.02987 seconds