Spo*_*pot 1 php global-variables
我们使用实例化并将系统关键对象放入其中$GLOBALS以便从任何地方轻松访问(例如DB,Cache,User等).
我们使用了$GLOBALS很多,如果我可以像$G = &$GLOBALS速记电话一样引用它,那么(是的,真的)会减少相当多的代码量.
问题是,每次我的经验和几个小时的谷歌搜索的,我还没有发现任何PHP结构,它允许你"标志"一个变种全球,使$GLOBALS一流的,和一切第二类.
我错过了什么吗?这可能吗?
<?php
function &G($name) {
if(func_num_args() > 1) {
$GLOBALS[$name] = func_get_arg(1);
}
return $GLOBALS[$name];
}
G('test', 'hey');
echo G('test'); // outputs hey
echo $test; // outputs hey
$b =& G('test');
$b = 'hello';
echo G('test'); // outputs hello
echo $test; // outputs hello
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2742 次 |
| 最近记录: |