ArrayAccess(数组式访问)接口
接口摘要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
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。