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. 7 1736
.oO(John) 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).
PHP will show a notice when attempting to read an unitialized variable.
This requires error_reporting set to E_ALL, which is not the default,
but a _must_ on a development system.
Another option to avoid globals could be OOP.
Micha
On Fri, 25 Feb 2005 13:41:08 +0000, John wrote: If I put all my globals into an array/hashtable, eg.
You could use $_SESSION for that. Not sure if it requires a
session_start() though, never used it without.
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
As a C developer myself I don't like the use of globals, so i just pass
the variables into a function. A lot of my code is split up into
functions so I don't have much of a problem.
But #2 is pretty good too.
Rushi
John wrote: Hi,
Hi yerself, Could 'SUPERGLONAL' be added to PHP, to decalre a variable to be superglobal ?
Already there. Only it's called $GLOBALS
C.
Colin,
$GLOBALS is just a list of your global variables - it does not
make them 'superglobal'.
John. Could 'SUPERGLONAL' be added to PHP, to decalre a variable to be superglobal ?
Already there. Only it's called $GLOBALS
C.
On Fri, 25 Feb 2005 13:41:08 +0000, John <jo********@mini-net.co.uk>
wrote: 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).
As others have mentioned, if you set your error level high enough
(E_ALL) than you'll get notices for any undefined variable accesses.
But that's still not the greatest solution.
Whenever I use globals I always use the $GLOBALS array and not the
global keyword.
In PHP5, I don't use "globals" at all instead I use static class
variables which avoids all these scoping issues.
I prefer the idea of (2), but I wonder if its just going too far ?
Seriously, you want to use objects and classes. You can avoid using
globals for nearly everything if your project is propertly organized
into classes. I have a HUGE project and I generally only use globals
for temporary caching.
Would anyone suggest what the prefered way of having and using globals is ?
I believe the $GLOBALS array is preferred over the global keyword. In
fact, I haven't used the global keyword in a very a long time.
Later,
"John" <jo********@mini-net.co.uk> wrote in message
news:11*************@corp.supernews.com... 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 ?
You can't create your own superglobal, though what you can do is hijack one
of the existing ones:
$_ENV = $mystuff;
Not a civil way to program though.
Being an old timer, I consider superglobals sacrilegious. The global keyword
was one of PHP's key innovations. The language actually discourages you from
using global variable. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Frostillicus |
last post by:
I'm trying to use array_multisort to sort by one of the dimensions of an
array stored in $GLOBALS like this:
array_multisort($GLOBALS, SORT_STRING, SORT_DESC);
Each "row" in $GLOBALS contains...
|
by: Kerim Borchaev |
last post by:
Hello!
Always when I use "super" I create a code duplication because class
used as first arg to "super" is always the class where the method
containing "super" was defined in:
'''
class C:...
|
by: Clarence Gardner |
last post by:
The super object is considered a solution to the "diamond problem".
However, it generally requires that the ultimate base class know that it
is last in the method resolution order, and hence it...
|
by: Nicolas Lehuen |
last post by:
Hi,
I hope this is not a FAQ, but I have trouble understanding the behaviour of
the super() built-in function. I've read the excellent book 'Python in a
Nutshell' which explains this built-in...
|
by: Delaney, Timothy C (Timothy) |
last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286195
This is a new version of super that automatically determines which
method needs to be called based on the existing stack frames....
|
by: Steven Bethard |
last post by:
When would you call super with only one argument? The only examples I
can find of doing this are in the test suite for super. Playing around
with it:
py> class A(object):
.... x = 'a'...
|
by: hexerror |
last post by:
Hello,
I am currently having an issue with accessing any type of Super Global
variable from within my scripts. The issue occurs when I attempt to get
the data from a Super Global variable from...
|
by: xml0x1a |
last post by:
How do I use exec?
Python 2.4.3
----
from math import *
G = 1
def d():
L = 1
exec "def f(x): return L + log(G) " in globals(), locals()
f(1)
|
by: cokofreedom |
last post by:
if __name__ == '__main__':
print "Globals (For Loop):"
try:
for i in globals():
print "\t%s" % i
except RuntimeError:
print "Only some globals() printed\n"
else:
print "All globals()...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |