PHP GLOBALS注册本地变量问题
大家好, 最近我在分析osCommerce3这套源码时发现一个特性:他在业务模块中会把PDO等对象注册到一个Registry类中:
除了注册到本地,还在GLOBALS中注册了一份。
在返回到最上层的时候我发现原本注册到GLOBALS中的对象自动加载到本地变量中了。这是什么特性吗? 或者属于何种设计模式呢?
我写了一个简化版:
index.php
<?php
require("os.php");
os::init();
?>
os.php
<?php
require_once("reg.php");
class os {
static $data = [];
public static function init() {
reg::set("A", new A());
reg::set("B", new B());
}
}
class A{}
class B {}
?>
reg.php
<?php
class reg {
static $data = array();
static public function get($key) {
return self::$data[$key];
}
static public function set($key, $value) {
$GLOBALS[$key] = self::$data[$key] = $value;
}}
谢谢~