If you want the object to be used like a PHP array, then we need to implement the Arrayaccess interface.
Arrayaccess is a interface to implement this interface, there are several methods that must be implemented:
interfaceArrayAccess{//标示一个元素是否定义publicfunctionoffsetExists($offset);//返回一个元素值publicfunctionoffsetGet($offset);//为一个元素赋值publicfunctionoffsetSet($offset,$value);//删除一个元素值publicfunctionoffsetUnset($offset);}
Realize:
classTestimplementsarrayaccess{Private$elements=Array();//Indicates whether an element is defined Public functionoffsetexists($offset) {returnisset($this->elements[$offset]); }//Returns an element value Public functionoffsetget($offset) {return$this->elements[$offset]; }//Assign a value to a single element Public functionoffsetset($offset,$value) {$this->elements[$offset] =$value; }//Delete an element value Public functionoffsetunset($offset) {unset($this->elements[$offset]); }}$test=NewTest ();$test[' Test '] =' Test ';//Automatic call Offsetsetif(isset($test[' Test ']))//Automatic call offsetexists{Echo$test[' Test '];//Automatic call OffsetgetEcho'
';unset($test[' Test ']);//Automatic call OffsetunsetVar_dump ($test[' Test ']);}
classobjimplementsarrayaccess{Private$container=Array(); Public function__construct() {$this->container =Array(' One '=1,' Tow '=2,' three '=3, ); } Public functionoffsetget($offset) {returnisset($this->container[$offset]) ?$this->container[$offset] :NULL; } Public functionoffsetset($offset,$value) {if(Is_null ($offset)){$this->container =$value; }Else{$this->container[$offset] =$value; } } Public functionoffsetexists($offset) {returnisset($this->container[$offset]); } Public functionoffsetunset($offset) {unset($this->container[$offset]); }}$obj=NewObj;var_dump (isset($obj["both"]));//Call offsetexistsVar_dump ($obj["both"]);//Call Offsetgetunset($obj["both"]);//Call OffsetunsetVar_dump (isset($obj["both"]));//Call offsetexists$obj["both"] ="A value";//Call OffsetsetVar_dump ($obj["both"]);//Call Offsetget$obj[] =' Append 1 ';$obj[] =' Append 2 ';$obj[] =' Append 3 ';p Rint_r ($obj);
'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
'). Text (i)); }; $numbering. FadeIn (1700); }); });
The above describes the 1ArrayAccess (array access) interface, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.