I have some questions about the implementation of implementing ArrayAccess
in PHP.
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?$obj["two"]
the functions are not be called from outside.__constructor
function? This is the constructor function I know but in this case what kind of help it is being of.ArrayAccess
and ArrayObject
? I am thinking the class I implemented by inheriting the ArrayAccess
doesn't support iteration?ArrayAccess
?Thanks...
ArrayAccess
is an interface, ArrayObject
is a class (which itself implements ArrayAccess
)* Your constructor could look like this
public function __construct( array $data ) {
$this->container = $data;
}