Hi,
I'm looking for the best way to deal with globals in PHP.
As a 'C' software developer, I would normally avoid all
globals and not have any at all, but use structs and pass
everything in the function parameters...
However, being realistic, I can see that globals can (and
do ?) have a place in PHP web scripts.
As I see it, there are two approaches:
1) go with globals,
2) avoid globals.
Looking at (1)
==============
Sure, I can just put a 'global $a' at the start of every
function that requires the use of the global '$a', but its
an easy mistake to forget this, and would not cause an error
('$a' will be read as NULL).
So, is there a way for a script to declare that a variable
should be a 'superglobal', just like $_SERVER is.
If not - could this be a useful addition to PHP ?
Looking at (2)
==============
If I put all my globals into an array/hashtable, eg.
$superglobal['a'] = 'hello world';
then this emulates the idea of a 'struct' in 'C', and I can
pass it through all my functions (or not bother with doing
that and use it as my one and only global).
Questions
=========
I prefer the idea of (2), but I wonder if its just going too
far ? Would anyone suggest what the prefered way of having and
using globals is ?
Could 'SUPERGLONAL' be added to PHP, to decalre a variable to be
superglobal ?
All thoughts welcome...
John.
Comment