note 77328 modified in language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:34:09 +0000 |
| Subject: | note 77328 modified in language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155552@lists.php.net to get a copy of this message | ||
Another way of dealing with a large number of globals is to declare a single global array and then
put all your global variables into it. Like this:
<?php
$_G = array(
'foo' => 'some text',
'bar' => 4,
'boo' => 'more text,
'far' => 'yet more text'
);
?>
Now you just declare the one global array in each function:
<?php
function blah() {
global $_G;
echo $_G['foo']; // or whatever
}
?>
You can freely add to it without having to go back and add variable declarations to your functions.
Kinda like using the $GLOBALS superglobal, except you don't have to type so much.
--was--
Another way of dealing with a large number of globals is to declare a single global array and then
put all your global variables into it. Like this:
$_G = array(
'foo' => 'some text',
'bar' => 4,
'boo' => 'more text,
'far' => 'yet more text'
);
Now you just declare the one global array in each function:
function blah() {
global $_G;
echo $_G['foo']; // or whatever
}
You can freely add to it without having to go back and add variable declarations to your functions.
Kinda like using the $GLOBALS superglobal, except you don't have to type so much.
http://php.net/manual/en/language.variables.scope.php