English 中文(简体)
Make all variables global, PHP
原标题:

Is there a way to make all variables global?

问题回答

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 ${"x00x00"} => ${"x00x01"})
{
    $GLOBALS[${"x00x00"}] =&$${"x00x00"};
}

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 ${"x00x00"} => ${"x00x01"})
    {
        $GLOBALS[${"x00x00"}] =&$${"x00x00"};
    }
}

$foo = 1;
bar();
echo  $goo:  , var_dump($goo); # int(1)
echo  $foo:  , var_dump($foo); # int(2)
echo  $baz:  , var_dump($baz); # int(3)

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.

Quick and dirty way:

$GLOBALS += get_defined_vars();

I don t know if this hack is portable (it works on PHP 5.3.1) and I suspect the objects are cloned.

I think this can help foreach($GLOBALS as $name => $value) global $$name; put it inside the function and you ll have all defined variables visibles

You Can simple make them a reference to $GLOBALS

foreach($GLOBALS as $k => $v)
   $$k=&$GLOBALS[$k];

Explanation of the code:

$GLOBLAS is a superglobal variable (visible everywhere). Basicly it contains all variables


$$ means the variable with the name of the value of the variable you wrote

Bit weird to explain in a foreign language so here s an example:

$color= blue ;
$blue= foo ;

echo $$color;

will output

foo

$k=& $v;

means that $k is a reference to $v

This is really not a good practice to make all variables GLOBAL with respect to security. To add variables as global you will have to add GLOBAL keyword each time you are going to access. Alternative approach is the use of array $GLOBAL.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签