This article mainly introduces the PHP array-type Access interface arrayaccess usage, combined with the example form analysis of the array of Access interface Arrayaccess concept, function, implementation and use of the method, the need for friends can refer to the following
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-> ;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
Articles you may be interested in:
Analysis of PHP iterator interface iterator usage
The usage analysis of PHP aggregation iterator interface Iteratoraggregate
PHP Detection Interface traversable use of detailed