Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make all variables global, PHP

Is there a way to make all variables global?

like image 967
tarnfeld Avatar asked Dec 15 '09 19:12

tarnfeld


People also ask

How do I create a global variable in PHP?

So, a global variable can be declared just like other variable but it must be declared outside of function definition. Syntax: $variable_name = data; Below programs illustrate how to declare global variable.

Are PHP variables global?

Some predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special. The PHP superglobal variables are: $GLOBALS.

What are PHP auto global variables?

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods. Unlike all of the other superglobals, $GLOBALS has essentially always been available in PHP.

How do you declare a global variable?

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.


2 Answers

To import all global variables incl. superglobals and clashing names of parameters into the functions scope:

extract($GLOBALS, EXTR_REFS | EXTR_SKIP);

The problem is with the superglobals here. You might want to exclude them, here is a list (PHP 5.2):

/**
 * PHP Superglobals
 */
array (
  1 => 'GLOBALS',
  2 => '_ENV',
  3 => 'HTTP_ENV_VARS',
  4 => '_POST',
  5 => 'HTTP_POST_VARS',
  6 => '_GET',
  7 => 'HTTP_GET_VARS',
  8 => '_COOKIE',
  9 => 'HTTP_COOKIE_VARS',
  10 => '_SERVER',
  11 => 'HTTP_SERVER_VARS',
  12 => '_FILES',
  13 => 'HTTP_POST_FILES',
  14 => '_REQUEST',
  15 => 'HTTP_SESSION_VARS',
  16 => '_SESSION',
)

You get the parameter variable names with get_defined_vars.

That's also the reason why the opposite is less tricky, get_defined_vars does not return the superglobals, only the local variables.

The global creates a reference to the variable of the global scope, so it's actually a local variable that is an alias to the global variable with the same name. Also some local vars are clashing to export, so some pre-cautions like esoteric variable names should be taken:

foreach(get_defined_vars() as ${"\x00\x00"} => ${"\x00\x01"})
{
    $GLOBALS[${"\x00\x00"}] =&$${"\x00\x00"};
}

Note that like globals the $GLOBALS superglobal array contains references to the global variables as well, so this creates references here as well. This is especially needed if you import via global or &$GLOBALS[...] or the extract like above. Or if you have local variables that are aliases to private class members (don't do that ;)):

Example/Demo:

<?php
/**
 * Make all variables global, PHP
 * @link http://stackoverflow.com/q/1909647/367456
 */
error_reporting(~0);

function bar($goo = 1)
{
    global $foo;

    $foo++;
    $baz = 3;

    foreach(get_defined_vars() as ${"\x00\x00"} => ${"\x00\x01"})
    {
        $GLOBALS[${"\x00\x00"}] =&$${"\x00\x00"};
    }
}

$foo = 1;
bar();
echo '$goo: ', var_dump($goo); # int(1)
echo '$foo: ', var_dump($foo); # int(2)
echo '$baz: ', var_dump($baz); # int(3)
like image 89
hakre Avatar answered Oct 12 '22 22:10

hakre


It doesn't matter what you're trying to do, but this is a bad way of going about it. You'll be much better off just passing variables as arguments to functions or by declaring them global there.

but in short, there is no simple way to do it without a lot of global statements.

like image 40
GSto Avatar answered Oct 13 '22 00:10

GSto