Usage Analysis of PHP Array Access Interface ArrayAccess

  • 2021-08-31 07:28:46
  • OfStack

This article illustrates the use of PHP array provider ArrayAccess. Share it for your reference, as follows:

The PHP ArrayAccess interface, also known as an array provider, provides the ability to access objects as if they were Array 1.

The interface is summarized as follows:


ArrayAccess {
  //  Get 1 The value of the offset position 
  abstract public mixed offsetGet ( mixed $offset )
  //  Settings 1 The value of the offset position 
  abstract public void offsetSet ( mixed $offset , mixed $value )
  //  Check 1 Does the offset position exist 
  abstract public boolean offsetExists ( mixed $offset )
  //  Reset 1 The value of the offset position 
  abstract public void offsetUnset ( mixed $offset )
}

Example illustration:


<?php
/**
* ArrayAndObjectAccess
*  This class allows access as an array or object 
*
* @author  Crazy old driver 
*/
class ArrayAndObjectAccess implements ArrayAccess {
  /**
   *  Definition 1 Arrays are used to hold 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 as an object 1 Array elements 
   *
   * @access public
   * @param string  Array element key name 
   * @param mixed  Array element value 
   * @return mixed
   */
  public function __set($key,$value) {
    $this->data[$key] = $value;
  }
  /**
   *  Determines whether an array element is set as an object 
   *
   * @access public
   * @param  Array element key name 
   * @return boolean
   */
  public function __isset($key) {
    return isset($this->data[$key]);
  }
  /**
   *  Delete as an object 1 Array elements 
   *
   * @access public
   * @param  Array element key name 
   */
  public function __unset($key) {
    unset($this->data[$key]);
  }
  /**
   *  Array to the data Array addition 1 Elements 
   *
   * @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;
    }
  }
  /**
   *  Get as an array data Array specifies position elements 
   *
   * @access public
   * @abstracting ArrayAccess
   * @param  Offset position 
   * @return mixed
   */
  public function offsetGet($offset) {
    return $this->offsetExists($offset) ? $this->data[$offset] : null;
  }
  /**
   *  Determine whether the offset position element is set in array mode 
   *
   * @access public
   * @abstracting ArrayAccess
   * @param  Offset position 
   * @return boolean
   */
  public function offsetExists($offset) {
    return isset($this->data[$offset]);
  }
  /**
   *  Delete as an array data Array specifies position elements 
   *
   * @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::offsetSet
var_dump(isset($animal->dog)); //  Call ArrayAndObjectAccess::__isset
var_dump(isset($animal['pig'])); //  Call ArrayAndObjectAccess::offsetExists
var_dump($animal->pig); //  Call ArrayAndObjectAccess::__get
var_dump($animal['dog']); //  Call ArrayAndObjectAccess::offsetGet
unset($animal['dog']); //  Call ArrayAndObjectAccess::offsetUnset
unset($animal->pig); //  Call ArrayAndObjectAccess::__unset
var_dump($animal['pig']); //  Call ArrayAndObjectAccess::offsetGet
var_dump($animal->dog); //  Call ArrayAndObjectAccess::__get
?>

Above output:


boolean true
boolean true
string 'pig' (length=3)
string 'dog' (length=3)
null
null

More readers interested in PHP can check the topic of this site: "PHP Array (Array) Operation Skills Encyclopedia", "PHP Common Traversal Algorithms and Skills Summary", "php String (string) Usage Summary", "php Common Functions and Skills Summary", "PHP Error and Exception Handling Methods Summary", "PHP Basic Syntax Introduction Course", "php Object-Oriented Programming Introduction Course", "php+mysql Database Operation Introduction Course" and "php Common Database Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: