使所有变量全局化,PHP [英] Make all variables global, PHP
问题描述
有没有办法让所有变量全局化?
导入所有全局变量,超级全局变量和参数名称冲突到函数范围:
extract($ GLOBALS,EXTR_REFS | EXTR_SKIP);
问题在于 superglobals here。您可能想排除它们,这里是一个列表(PHP 5.2):
/ **
* PHP Superglobals
* /
数组(
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',
)
您可以通过 get_defined_vars
。
这也是为什么反过来不那么棘手的原因, get_defined_vars
不返回超全球变量,只返回局部变量。
$ b $
global
创建对全局范围变量的引用,所以它实际上是一个局部变量,它是具有相同名称的全局变量的别名。 foreach(get_defined_vars(get_defined_vars( )as $ {\x00\x00} => $ {\x00\x01})
{
$ GLOBALS [$ {\x00\x00 }] =& $$ {\x00\x00};
请注意,像全局变量
$ GLOBALS
superglobal数组也包含对全局变量的引用,所以这也在这里创建引用。如果您通过全球
或& $ GLOBALS [...]
或提取
,就像上面一样。或者如果你有私有类成员的别名的局部变量(不这样做)):
示例/ Demo :
<?php
/ **
*使所有变量成为全局变量,PHP
* @link http://stackoverflow.com/q/1909647/367456
* /
error_reporting(〜0);
函数栏($ 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)
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 ${"\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)
这篇关于使所有变量全局化,PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!