dt******@hotmail.com (mrbog) wrote in
news:cb**************************@posting.google.c om:
I have an array/hash that stores path information for my app. As in,
what directory this is in, what directory that's in, what the name of
the site is, what the products are called, etc. It's called $glb.
So, every function so far looks like this:
function something() {
global $glb;
}
over and over and over
How do I make $glb just always be global? I'm assuming there's a way
to do this, otherwise let me know and I'll parachute out of this
language and run back home to perl.
Hmm. If you had crossposted this to comp.lang.perl.misc, most of the
regulars there would have told you that regardless of what language you're
using, you need to rethink your programming style if your functions/subs
have to access more than a handful of globals. It's simply a fact of life
(often learned through bitter experience) that when you have lots of
functions that communicate with each other through global variables, things
very rapidly get messy and unmaintainable because your code depends on
"action at a distance."
Six months after you write a program, you need to change the code in a
function, look at it and see a bunch of "global..." and now you have to
look at every other function that uses those variables to make sure their
behavior won't be adversely affected by any changes you make (it's even
worse in Perl, since global variables are accessible inside subs unless you
explicitly override them, so Perl regulars who have got bitten by this
sort of thing will yell at you if you don't give your variables the
narrowest scope possible). And if you ever have to ask yourself "can I
give this name to my new variable without stepping on some other code"
you're suffering from Creeping Globalism.
OOP (specifically the encapsulation aspects) is one way of getting around
the "action at a distance" problem but it's not, contrary to what some
dogmatists might say, the only way. Every common programming language, for
example, allows you to pass parameters by reference, and you can make good
use of that by passing references to shared parameters.
So basically, what you're asking for is a facility to give yourself enough
rope to hang yourself. Perl, to some extent, does that, but a lot of Perl
programmers misunderstand the reason for it. Larry Wall was aware of the
fact that there are many forms of programming discipline and that, despite
all the religious wars, what really matters is that you pick a particular
form of programming discipline and stick to it. The naive Perl programmer
regards Perl as an "undisciplined" language. The experienced Perl
programmer regards Perl as a "bring your own discipline (BYOD)" language.
The lesson here is applicable to other languages, including PHP. If your
functions are heavily dependent on global variables, you need to refactor
your code.