随机数据容器使用魔术方法__get和__set

时间:2015-04-06 17:37:44

标签: php containers magic-methods

是否有可能使用魔术方法创建一个PHP类,它可以保存你抛出的任何类型的数据,甚至是递归的?

我看到了这个:PHP - Indirect modification of overloaded property

但它不处理递归数据:

class ActiveRecord extends Creator {

}

$a = new ActiveRecord();

$a->_id = "123456789";
$a->persona_info = [
    "name" => "Bob",
    "surnames" => ["First", "Second", "Third"]
];
$a->history = [
    "logins" => [
        [
            "date" => "1999",
            "ip" => "1.2.3.4"
        ],
        [
            "date" => "1129",
            "ip" => "1.2.3.4"
        ]
    ],
    "purchases" => [
        [
            "date" => "1819",
            "amount" => "1884"
        ],
        [
            "date" => "1459",
            "amount" => "14"
        ]
    ]
];

var_dump($a->history->logins);

这让我:

PHP Notice:  Trying to get property of non-object in /tmp/x.php on line 90
PHP Stack trace:
PHP   1. {main}() /tmp/x.php:0
NULL

尝试进一步调查,我发现$a->history是一个普通的php数组而不是Value个对象(甚至是Creator个对象。

1 个答案:

答案 0 :(得分:-1)

问题是$a->historyarray而不是object。它应该是var_dump($a->history['logins']);