[英]ArrayAccess Walkthrough with Questions
I have some questions about the implementation of implementing ArrayAccess
in PHP.我对在
ArrayAccess
中实现 ArrayAccess 的实现有一些疑问。
Here is the sample code:这是示例代码:
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;
}
}
Questions:问题:
ArrayAccess
since I am assuming it is special interface that PHP Engine recognizes and calls the implemented inherited functions automatically?ArrayAccess
,因为我假设它是 PHP 引擎自动识别并调用实现的继承函数的特殊接口?$obj["two"]
the functions are not be called from outside.$obj["two"]
时不会从外部调用函数。__constructor
function?__constructor
function 中分配填充数组是否有特殊原因? This is the constructor function I know but in this case what kind of help it is being of.ArrayAccess
and ArrayObject
? ArrayAccess
有什么ArrayObject
? I am thinking the class I implemented by inheriting the ArrayAccess
doesn't support iteration?ArrayAccess
不支持迭代?ArrayAccess
?ArrayAccess
的情况下实现对象索引? Thanks...谢谢...
ArrayAccess
is an interface , ArrayObject
is a class (which itself implements ArrayAccess
) ArrayAccess
是一个interface , ArrayObject
是一个 class (它本身实现ArrayAccess
)* Your constructor could look like this * 你的构造函数可能看起来像这样
public function __construct( array $data ) {
$this->container = $data;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.