企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
ArrayAccess 的作用是使得你的对象可以像数组一样可以被访问; 通过创建这四个方法实现对象像数组一样可以被访问 offsetExists ($offset) //检查偏移位置是否存在 offsetGet ($offset) //获取一个偏移位置的值 offsetSet ($offset ,$value) //设置一个偏移位置的值 offsetUnset ($offset) //复位一个偏移位置的值 ``` <?php class Test implements ArrayAccess { private $testData; public function offsetExists($key) { return isset($this->testData[$key]); } public function offsetSet($key, $value) { $this->testData[$key] = $value; } public function offsetGet($key) { return $this->testData[$key]; } public function offsetUnset($key) { unset($this->testData[$key]); } } $obj = new Test(); //自动调用offsetSet方法 $obj['data'] = 'data'; //自动调用offsetExists if(isset($obj['data'])){ echo 'has setting!'; } //自动调用offsetGet var_dump($obj['data']); //自动调用offsetUnset unset($obj['data']); var_dump($test['data']); //输出: //has setting! //data //null var_dump($obj); 打印结果是一个对象 ```