我正在尝试存储一个数组并使用扩展 ArrayObject 的自定义类来操作该数组。
class MyArrayObject extends ArrayObject {
protected $data = array();
public function offsetGet($name) {
return $this->data[$name];
}
public function offsetSet($name, $value) {
$this->data[$name] = $value;
}
public function offsetExists($name) {
return isset($this->data[$name]);
}
public function offsetUnset($name) {
unset($this->data[$name]);
}
}
问题是如果我这样做:
$foo = new MyArrayObject();
$foo['blah'] = array('name' => 'bob');
$foo['blah']['name'] = 'fred';
echo $foo['blah']['name'];
输出是 bob 而不是 fred。有什么办法可以在不改变上面 4 行的情况下让它工作吗?