【问题标题】:How to use arrays as __set __get hidden variable?如何使用数组作为 __set __get 隐藏变量?
【发布时间】:2014-02-24 02:55:57
【问题描述】:

我有课

class Session {
    public $storage;
    private function __construct($storage) {
        $this->storage = $storage ? $storage : new stdClass();
    }
    function __get($name) {
        return $this->storage->$name;
    }
    function __set($name, $value) {
        $this->storage->$name = $value;
    }
    function __isset($name) {
        return isset($this->storage->$name);
    }
}

我想以数组为键:

    $id = uniqid('res_');
    if (!isset($session->mysql)) {
        $session->mysql = array();
    }
    $session->mysql[$id] = array(
        'host' => $host,
        'user' => $username,
        'pass' => $password,
        'name' => $db);

但出现错误:Indirect modification of overloaded property Session::$mysql has no effect 我尝试使用参考将其存储在变量中:

$mysql = &$session->mysql[$id]

但同样的错误,如果我不使用& 我没有错误但数据没有保存。

我怎样才能使它工作?我想像树一样访问存储变量。

【问题讨论】:

    标签: php magic-methods


    【解决方案1】:

    找到了,我要用

    function &__get($name) {
    }
    

    它工作。

    【讨论】:

      猜你喜欢
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 2014-12-02
      • 2011-03-20
      • 2011-06-10
      • 1970-01-01
      • 2011-03-07
      相关资源
      最近更新 更多