Php-arrayaccess Interface Detailed

Source: Internet
Author: User
The PHP arrayaccess interface is also called an array-style access interface that provides the ability to access an object like an array.

The interface summary is as follows:

arrayaccess {    //Get an offset position value    abstract public mixed offsetget (mixed $offset)    //Set an offset value for    abstract publ IC void Offsetset (mixed $offset, mixed $value)    //Check an offset position for the presence of an    abstract public boolean offsetexists (mixed $o Ffset)    //reset value of an offset position    abstract public void Offsetunset (mixed $offset)}

Example Description:

 data[$key]; /** * Add an array element as an object * * @access public * @param string array element key Name * @param mixed array element value * @retur    N Mixed */Public function __set ($key, $value) {$this->data[$key] = $value; /** * Determines whether an array element is set in object mode * * @access public * @param array element Key Name * @return Boolean */Public funct    Ion __isset ($key) {return isset ($this->data[$key]);        /** * Delete an array element as an object * * @access public * @param array element Key Name */Public Function __unset ($key) {    unset ($this->data[$key]);  }/** * Adds an array of elements to the data array * * @access public * @abstracting arrayaccess * @param string offset position * @param mixed Element value */Public Function Offsetset ($offset, $value) {if (Is_null ($offset)) {$this-&        Gt;data[] = $value;        } else {$this->data[$offset] = $value; }}/** * Array to get data array specified position element * * @access public * @abstracting ARrayaccess * @param offset position * @return Mixed */Public Function Offsetget ($offset) {return $this ->offsetexists ($offset)?    $this->data[$offset]: null;  /** * Array to determine if the offset position element is set * * @access public * @abstracting arrayaccess * @param offset position * @return    Boolean * Public Function offsetexists ($offset) {return isset ($this->data[$offset]);     /** * Delete data array as array specify position element * * @access public * @abstracting arrayaccess * @param offset Position * * Public Function Offsetunset ($offset) {if ($this->offsetexists ($offset)) {unset ($this->data[$        Offset]); }}} $animal = new arrayandobjectaccess (); $animal->dog = ' dog '; Call arrayandobjectaccess::__set$animal[' pig '] = ' pig '; Call Arrayandobjectaccess::offsetsetvar_dump (Isset ($animal->dog)); Call Arrayandobjectaccess::__issetvar_dump (Isset ($animal [' pig ']); Call Arrayandobjectaccess::offsetexistsvar_dump ($animal->pIG); Call Arrayandobjectaccess::__getvar_dump ($animal [' dog ']); Call Arrayandobjectaccess::offsetgetunset ($animal [' dog ']); Call Arrayandobjectaccess::offsetunsetunset ($animal->pig); Call Arrayandobjectaccess::__unsetvar_dump ($animal [' pig ']); Call Arrayandobjectaccess::offsetgetvar_dump ($animal->dog); Call arrayandobjectaccess::__get?>

Above output:

Boolean true
Boolean true
String ' Pig ' (length=3)
String ' dog ' (length=3)
Null
Null

The above describes the Php-arrayaccess interface in detail, 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.