Re: Re: Question about superglobals
| From: | Rasmus Lerdorf | Date: | Sat, 17 Nov 2007 04:34:00 +0000 |
| Subject: | Re: Re: Question about superglobals | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-33201@lists.php.net to get a copy of this message | ||
Sam Barrow wrote:
> You say that superglobals were not designed to be user defined, think
> about it, the concept of a superglobal is present in C and C++, two of
> the maturest and strictest languages around.
The concept of having to declare your globals is unique to PHP, true.
It is one of the oldest features in PHP too. So, a little history...
Back in 1989 I spent at least a month chasing a bug at a company in
Canada called Bell Northern Research. It was a weird crash bug in a
rather complicated system. It was mostly written in C and after pouring
through reams and reams of code printouts day after day, I finally found
that it was due to a side effect of some obscure function overwriting a
global variable that was used in a completely different area of the
code. That particular feature of the C language was not popular with me
for a long time after that, and when it came time to work out function
scoping of variables in PHP, I made sure to not make that same mistake.
And yes, because PHP is a web language and there are common aspects of a
web request that many functions will need to act on, it makes sense to
have a finite number of clearly labeled global arrays like _GET, _POST,
etc. And later on, being able to access any global via $_GLOBALS was
added, but again, it is a very descriptive name and not likely to result
in someone mistaking it for something else and ending up with strange
side effects.
I am definitely not in favor of destroying what I consider a rather good
design decision just to avoid a couple of keystrokes here and there. So
yes, you will have to keep adding "global $cfg;" to your functions, or
accessing it via $_GLOBALS['cfg'], and when you forget, it won't work.
And yes, you will grumble a bit at that, but that is nothing compared to
trying to find a bug caused by a global side effect in someone else's
code. Trust me, you will grumble a lot more at that.
-Rasmus