PHP Array Access interface arrayaccess using the detailed

Source: Internet
Author: User
This time to bring you the PHP array access interface arrayaccess use in detail, the use of PHP array interface arrayaccess considerations, the following is the actual case, together to see.

This example describes the PHP array-style access interface arrayaccess usage. Share to everyone for your reference, as follows:

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:

<?php/*** arrayandobjectaccess* This class allows access as an array or an object * * @author crazy old driver */class Arrayandobjectaccess implements  arrayaccess {/** * defines an array to hold the data * * @access private * @var array */private $data = []; /** * Accessing data in an array by object * * @access public * @param string array element Key Name */Public function get ($key) {return $this-&G  t;data[$key]; }/** * Add an array element as an object * * @access public * @param string array element key Name * @param mixed array element value * @return Mixed */PU  Blic 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 function 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->d  ata[$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->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 by array specify position element * * @access public * @abstracting arrayaccess * @param offset position */Public function O    Ffsetunset ($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 Trueboolean truestring ' pig ' (length=3) string ' dog ' (length=3) nullnull

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP callback function using the detailed

How to Count binary algorithms in PHP

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.