Arrayaccess (array-access) interface

Source: Internet
Author: User

Interface Summary Arrayaccess {/* method */abstract public boolean offsetexists (mixed $offset) abstract public mixed offsetget (mixed $o Ffset) abstract public void Offsetset (mixed $offset, mixed $value) abstract public void Offsetunset (mixed $offset)}


As an example,

class test implements arrayaccess{    private  $testData  = [] ;     public function offsetexists ($offset)     {         echo  ' call  '  . __METHOD__ .  ' \ r \ n ';         return isset ($this->testdata[$offset]);     }    public function offsetget ($offset)     {         echo  ' call  '  . __METHOD__ .  ' \ r \ n ';         return  $this->testdata[$offset];     }    public function offsetset ($offset,  $value)      {        echo  ' call  '  . __method__ .   "\ r \ n";         return  $this->testdata[$offset] =  $value;     }     public function offsetunset ($offset)     {         echo  ' call  '  . __METHOD__ .  ' \ r \ n ';         unset ($this->testdata[$offset]);     }}



$obj = new Test ();

if (!isset ($obj [' name '])) {//call test::offsetexists
$obj [' name '] = ' zhangsan '; Call Test::offsetset
}
echo $obj [' name '].    "\ r \ n"; Call Test::offsetget

Var_dump ($obj);

$obj [' age '] = 18; Call Test::offsetset


echo $obj [' age '].    "\ r \ n"; Call Test::offsetget


unset ($obj [' address ']); Call Test::offsetunset

Arrayaccess (array-access) interface

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.