Page 2 of 2
Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 2:16 pm
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.
Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 3:08 pm
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.
Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 3:12 pm
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".

Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 3:20 pm
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.
Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 4:53 pm
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

Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 5:18 pm
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.
Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 5:40 pm
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!