//ArrayAccess php内置 class ObjArray implements \ArrayAccess { private $testArray =[ "test" =>"gzh", ]; public function offsetExists($key){ echo "offsetExists".$key.PHP_EOL; return isset($this->testArray[$key]); } public function offsetGet($key){ echo "offsetGet".$key.PHP_EOL; return $this->testArray[$key]; } public function offsetSet($key,$value){ echo "offsetSet".$key.PHP_EOL; $this->testArray[$key] = $value; } public function offsetUnset($key){ echo "offsetUnset".$key.PHP_EOL; unset($this->testArray[$key]); } }
$obj = new \ObjArray(); $obj['age'] =12; unset($obj['test']); isset($obj['test']); var_dump($obj['age']);