Which is the best technic to use global arrays & variables ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Which is the best technic to use global arrays & variables ?

Post by infolock »

mmj wrote:
infolock wrote:global variables? I haven't used since 2004 i think.

only global variables i use are superglobal variables ;)
So you don't have one variable that can be accessed in the global scope?

Not possible.
I don't use globals. Dunno what else to tell you. I think you can view every line of code I have written in the past 4 years and you won't see the keyword GLOBAL anywhere. Just never needed them (don't think I'll ever need them really, but one never really knows I guess).

So, no, not impossible at all, but very much reality ;)

But it's really according to what your definition of a "global scope" is and what mine is. If you are referring to a string of includes that you'll eventually call a variable from another file, that isn't exactly the same as defining something "global" or throwing it into the superglobal $GLOBALS array.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Which is the best technic to use global arrays & variables ?

Post by josh »

mmj wrote:
infolock wrote:global variables? I haven't used since 2004 i think.

only global variables i use are superglobal variables ;)
So you don't have one variable that can be accessed in the global scope?

Not possible.
Global variables go against good design principles, because when you are writing a program you are writing a specification for a concept that the computer can execute. A good design is arguably defined by its adaptability to change, ease of maintained, and ease of re-use / extendability. Let's say you write the software for a blog, you're writing a specification for that. If your specification code depends on a hard coded variable, its harder to come in and change that specification so it specifics, for example.. an array of blogs. Things need to be encapsulated so multiple instances of your "concepts" can coexist.

Variable scopes are in every programming language for a reason. Sure global variables can come in handy, but in the same way jumping out of a moving vehicle is a "handy" way to save some time exiting the car on your way home from work. Sure you saved some time, but now your car is being ghost ridden into a brick wall. ( totally irrelevant made up metaphor here, if you want more info on the real reasons not to use globals goto google ).

If an object is so fundamental that it used in every scope of the program, it belongs in a registry class, like lets say the system locale or the database connection.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Which is the best technic to use global arrays & variables ?

Post by mmj »

infolock wrote:I don't use globals. Dunno what else to tell you. I think you can view every line of code I have written in the past 4 years and you won't see the keyword GLOBAL anywhere. Just never needed them (don't think I'll ever need them really, but one never really knows I guess).

So, no, not impossible at all, but very much reality ;)

But it's really according to what your definition of a "global scope" is and what mine is. If you are referring to a string of includes that you'll eventually call a variable from another file, that isn't exactly the same as defining something "global" or throwing it into the superglobal $GLOBALS array.
Yeah, the correct way to style functions is for any necessary variables to be passed to it.

After all the whole point of functions is abstraction.

But when I spoke of globals I was referring to your first "definition". :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Which is the best technic to use global arrays & variables ?

Post by josh »

Another problem with globals ( or a better way to explain the problem ) is that a variable may very well be "global" today, but tomorrow you need a multi tier architecture, you might need each client to have a different database connection for example. If you put the variable in an object, you can then have multiple instances of each object, that each maintains their own state & settings, without having to moderate the instances talking to some "global" registry or array of settings.

If client A or codebase A changes variable $x, should that change variable $x for client B or codebase B? This design problem is the reason scopes were invented.

Registries use a design pattern called a singleton, and the singleton design pattern acts just like globals but since its an object when you need to go refactor and remove the singleton functionality its not a problem ( not saying singleton is the swiss army knife of design tools, there's better patterns for this problem but this is just the easiest to understand for a beginner ). The singleton holds it's own scope, since your code is now coupled to the interface of the registry object, instead of the actual scope, different instances of the registry can be substituted.

If you follow this, you will realize global variables are quite counterproductive to writing usable code.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Which is the best technic to use global arrays & variables ?

Post by pickle »

What about using OOP for this? Store the array as an object variable then reference it via $this-> in your object functions. It's pretty much same idea as using $_GLOBALS, but you won't get people complaining about you using global variables ;)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Which is the best technic to use global arrays & variables ?

Post by josh »

Well I think if the only reason you're doing it is because people told you to do it, you don't understand it and you're going to make even bigger mistakes. If you see the benefits of bein able to take one single global "state" and have multiple "instances" of it, all independent of each other, then by all means go learn to implement OOP, but if you're going to do it do it for a better reason then the devnet zealots telling you all globals are sinful ( they are, but prove it to yourself, don't take our word for it, but don't just discard our warnings either.. realize that we may be right and investigate it for yourself ).

In other words don't do it just to stop people from complaining. Pickle makes a good point I just wanted to elaborate on some of the ambiguity.
User avatar
kanenas.net
Forum Newbie
Posts: 12
Joined: Sun Jun 26, 2005 12:29 pm
Location: Athens - Greece
Contact:

Re: Which is the best technic to use global arrays & variables ?

Post by kanenas.net »

pickle wrote:What about using OOP for this? Store the array as an object variable then reference it via $this-> in your object functions. It's pretty much same idea as using $_GLOBALS, but you won't get people complaining about you using global variables ;)
I tried something like that and i think that is the best solution for me!
Post Reply