Interface Summary Arrayaccess {/* method */abstract public boolean offsetexists (mixed $offset) abstract public mixed offsetget (mixed $o Ffset) abstract public void Offsetset (mixed $offset, mixed $value) abstract public void Offsetunset (mixed $offset)}
As an example,
class test implements arrayaccess{ private $testData = [] ; public function offsetexists ($offset) { echo ' call ' . __METHOD__ . ' \ r \ n '; return isset ($this->testdata[$offset]); } public function offsetget ($offset) { echo ' call ' . __METHOD__ . ' \ r \ n '; return $this->testdata[$offset]; } public function offsetset ($offset, $value) { echo ' call ' . __method__ . "\ r \ n"; return $this->testdata[$offset] = $value; } public function offsetunset ($offset) { echo ' call ' . __METHOD__ . ' \ r \ n '; unset ($this->testdata[$offset]); }}
$obj = new Test ();
if (!isset ($obj [' name '])) {//call test::offsetexists
$obj [' name '] = ' zhangsan '; Call Test::offsetset
}
echo $obj [' name ']. "\ r \ n"; Call Test::offsetget
Var_dump ($obj);
$obj [' age '] = 18; Call Test::offsetset
echo $obj [' age ']. "\ r \ n"; Call Test::offsetget
unset ($obj [' address ']); Call Test::offsetunset
Arrayaccess (array-access) interface