1ArrayAccess (array-access) interface

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.