Example PHP array-type access interface arrayaccess

Source: Internet
Author: User
This article mainly introduces the PHP array-type Access interface arrayaccess usage, combined with the example form analysis of the array-type access interface arrayaccess concept, function, implementation and use of the method, the need for friends can refer to, hope to help everyone.

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 as an object * * @access public * @param string array element Key Name */Public Function __get ($key) {return $this-  >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-&gt  ;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 true
Boolean true
String ' Pig ' (length=3)
String ' dog ' (length=3)
Null
Null

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.