Watch out when you are trying to set $GLOBALS to the local variable.
Even without reference operator "&" your variable seems to be referenced to the $GLOBALS
You can test this behaviour using below code
<?php
/**
* Result:
* POST: B, Variable: C
* GLOBALS: C, Variable: C
*/
// Testing $_POST
$_POST['A'] = 'B';
$nonReferencedPostVar = $_POST;
$nonReferencedPostVar['A'] = 'C';
echo 'POST: '.$_POST['A'].', Variable: '.$nonReferencedPostVar['A']."\n\n";
// Testing Globals
$GLOBALS['A'] = 'B';
$nonReferencedGlobalsVar = $GLOBALS;
$nonReferencedGlobalsVar['A'] = 'C';
echo 'GLOBALS: '.$GLOBALS['A'].', Variable: '.$nonReferencedGlobalsVar['A']."\n\n";
$GLOBALS
(PHP 4, PHP 5)
$GLOBALS — Hace referencia a todas las variables disponibles en el ámbito global
Descripción
Es un array asociativo que contiene las referencias a todas la variables que están definidas en el ámbito global del script. Los nombres de las variables son las claves del array.
Ejemplos
Ejemplo #1 Ejemplo de $GLOBALS
<?php
function test() {
$foo = "variable local";
echo '$foo en el ámbito global: ' . $GLOBALS["foo"] . "\n";
echo '$foo en el ámbito simple: ' . $foo . "\n";
}
$foo = "Contenido de ejemplo";
test();
?>
El resultado del ejemplo sería algo similar a:
$foo en el ámbito global: Contenido de ejemplo $foo en el ámbito simple: variable local
Notas
Nota:
Esta es una 'superglobal' o una variable automatic global. Significa simplemente que es una variable que está disponible en cualquier parte del script. No hace falta hacer global $variable; para acceder a la misma desde funciones o métodos.
Nota: Disponibilidad de las variables
A diferencia de todas las otras superglobals, $GLOBALS ha estado esencialmente siempre disponible en PHP.
As of PHP 5.4 $GLOBALS is now initialized just-in-time. This means there now is an advantage to not use the $GLOBALS variable as you can avoid the overhead of initializing it. How much of an advantage that is I'm not sure, but I've never liked $GLOBALS much anyways.
I prefer accessing globals through static function calls. Source code looks better; I use glb::get('myglobalvar') instead of $GLOBALS['myglobalvar']. This gives me full control over global access, which can be the source of problems in practice.
class glb
{
static public function set($name, $value)
{
$GLOBALS[$name] = $value;
}
static public function get($name)
{
return $GLOBALS[$name];
}
}
$myglobalvar = 'Hello, World !';
function myfunction()
{
$val = glb::get('myglobalvar');
echo "$val\n";
glb::set('myglobalvar', 'hi, again :)');
$val = glb::get('myglobalvar');
echo "$val\n";
}
myfunction();
Keep in mind that $GLOBALS is, itself, a global variable. So code like this won't work:
<?php
print '$GLOBALS = ' . var_export($GLOBALS, true) . "\n";
?>
This results in the error message: "Nesting level too deep - recursive dependency?"
this is technique that i always did for configuration file..
<?php
$conf['conf']['foo'] = 'this is foo';
$conf['conf']['bar'] = 'this is bar';
function foobar() {
global $conf;
var_dump($conf);
}
foobar();
/*
result is..
array
'conf' =>
array
'foo' => string 'this is foo' (length=11)
'bar' => string 'this is bar' (length=11)
*/
?>
