接口摘要ArrayAccess{/*方法*/abstractpublicbooleanoffsetExists(mixed$offset)abstractpublicmixedoffsetGet(mixed$offset)abstractpublicvoidoffsetSet(mixed$offset,mixed$value)abstractpublicvoidoffsetUnset(mixed$offset)}


举个例子

classTestimplementsArrayAccess{private$testData=[];publicfunctionoffsetExists($offset){echo'call'.__METHOD__."\r\n";returnisset($this->testData[$offset]);}publicfunctionoffsetGet($offset){echo'call'.__METHOD__."\r\n";return$this->testData[$offset];}publicfunctionoffsetSet($offset,$value){echo'call'.__METHOD__."\r\n";return$this->testData[$offset]=$value;}publicfunctionoffsetUnset($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