如何在PHP注册表类中设置和获取嵌套数组


How to set and get nested arrays in a PHP registry class

我有一个PHP注册表类,方法是

public static function get($key) {
    return isset(self::$vars[$key]) ? self::$vars[$key] : null;
}
public static function set($key, $value = null) {
        self::$vars[$key] = $value;
}

问题是,我希望能够设置和获取嵌套数组,就像我通常会做的那样:

$array['a']['b'] = 'somevalue';
$myvalue = $array['a']['b'];

有什么想法吗?

数组可以定义为public:

public static $vars;

然后您将能够直接访问:

ClassName::$vars['a']['b'] = 'somevalue';
$myvalue = ClassName::$vars['a']['b'];

使用引用:

Storage::set("test", array('foo' => 'bar'));
$test = &Storage::get("test");
echo $test['foo']."'n"; // bar
$test['foo'] = "foobar";
$test2 = Storage::get("test");;
echo $test['foo']; // foobar

另请参阅这个示例phpfiddle。