<?phpclass obj implements arrayaccess {private $container = array (); Public Function __construct () {$this-container = Array ("One" = 1, " "= 2," three "= 3,); The Public Function Offsetset ($offset, $value) {echo ' executes the object when the array is assigned the same value, this method '; if (Is_null ($offset)) {$this, container [] = $value; } else {$this-container [$offset] = $value; The Public Function offsetexists ($offset) {echo ' executes the object when it is defined as an array, this method '; Return Isset ($this-Container [$offset]); The Public Function Offsetunset ($offset) {echo ' executes when an object is deleted as an array, this method '; unset ($this-Container [$offset]); The Public Function Offsetget ($offset) {echo ' takes an object as an array to fetch an element when executed, this method '; return Isset ($this COntainer [$offset])? $this-Container [$offset]: null; }} $obj = new obj; Var_dump (isset ($obj ["")]) var_dump ($obj ["" "]) unset ($obj ["] ") Var_dump (Isset ( $obj ["n"])), $obj ["" ""] = "a value"; Var_dump ($obj ["" "]); $obj [' A '] = ' Append 1 '; $obj [' b '] = ' Append 2 '; $obj [' c '] = ' Append 3 ';p rint_r ($obj);
PHP arrayaccess Array Access interface in pre-defined interface