关于在PHP中实现ArrayAccess的实现,我有一些疑问。
下面是示例代码:
class obj implements arrayaccess {
private $container = array();
public function __construct() {
$this->container = array(
"one" => 1,
"two" => 2,
"three" => 3,
);
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->container[] = $value;
} else {
$this->container[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->container[$offset]);
}
public function offsetUnset($offset) {
unset($this->container[$offset]);
}
public function offsetGet($offset) {
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
}问题:
ArrayAccess,因为我假设它是PHP自动识别和调用已实现的继承函数的特殊接口?$obj["two"]时,函数不是从外部调用的。__constructor函数中是否有特殊的理由分配填充数组?这是我所知道的构造函数,但在这种情况下,它是什么样的帮助。ArrayAccess和ArrayObject有什么区别?我认为通过继承ArrayAccess实现的类不支持迭代?ArrayAccess的情况下如何实现对象索引?谢谢..。
发布于 2011-06-10 19:33:00
ArrayAccess是一个接口,ArrayObject是一个类(它本身实现了ArrayAccess)*构造函数可能如下所示
public function __construct( array $data ) {
$this->container = $data;
}https://stackoverflow.com/questions/6311113
复制相似问题