Home  >  Article  >  Backend Development  >  PHP ArrayAccess interface

PHP ArrayAccess interface

王林
王林forward
2023-09-05 23:01:021438browse

PHP ArrayAccess 接口

Introduction

In PHP, the ArrayAccess interface is used to develop a class that provides a similar access to one of the array properties. Array access. Such an array property can be manipulated during object creation without exposing it. ArrayAccessThe interface defines the following abstract methods

Syntax

ArrayAccess {
   /* Methods */
   abstract public offsetExists ( mixed $offset ) : bool
   abstract public offsetGet ( mixed $offset ) : mixed
   abstract public offsetSet ( mixed $offset , mixed $value ) : void
   abstract public offsetUnset ( mixed $offset ) : void
}

Method

ArrayAccess::offsetExists - Whether the offset exists

ArrayAccess::offsetGet - The offset to retrieve

ArrayAccess::offsetSet - Assign a value to the specified offset

ArrayAccess::offsetUnset - Unset the offset.

ArrayAccess::offsetUnset - Unsets the offset. p>

ArrayAccess Example

In the following example, the associative array is an internal private property of myclass. The key acts as an offset. We can set, retrieve and unset items in an array. If no offset is given, it will be treated as an integer, incremented to the next index each time.

Example

Real-time demonstration

<?php
class myclass implements ArrayAccess {
   private $arr = array();
   public function __construct() {
      $this->arr = array(
         "Mumbai" => "Maharashtra",
         "Hyderabad" => "A.P.",
         "Patna" => "Bihar",
      );
   }
   public function offsetSet($offset, $value) {
      if (is_null($offset)) {
         $this->arr[] = $value;
      } else {
         $this->arr[$offset] = $value;
      }
   }
   public function offsetExists($offset) {
      return isset($this->arr[$offset]);
   }
   public function offsetUnset($offset) {
      unset($this->arr[$offset]);
   }
   public function offsetGet($offset) {
      return isset($this->arr[$offset]) ? $this->arr[$offset] : null;
   }
}
$obj = new myclass();
var_dump(isset($obj["Mumbai"]));
var_dump($obj["Mumbai"]);
unset($obj["Mumbai"]);
var_dump(isset($obj["Mumbai"]));
$obj["Bombay"] = "Maharashtra";
var_dump($obj["Bombay"]);
$obj["Chennai"] = &#39;Tamilnadu&#39;;
$obj[] = &#39;New State&#39;;
$obj["Hyderabad"] = &#39;Telangana&#39;;
print_r($obj);
?>

Output

The above program displays the following output

bool(true)
string(11) "Maharashtra"
bool(false)
string(11) "Maharashtra"
myclass Object(
   [arr:myclass:private] => Array(
      [Hyderabad] => Telangana
      [Patna] => Bihar
      [Bombay] => Maharashtra
      [Chennai] => Tamilnadu
      [0] => New State
   )

)

The array attribute of the class can also be an indexed array . In this case, the index of the element (starting from 0) acts as the offset. When the offsetSet(0 method is called without the offset parameter, the array index is incremented to the next available integer

Example

<?php
class myclass implements ArrayAccess {
   private $arr = array();
   public function __construct() {
      $this->arr = array("Mumbai", "Hyderabad", "Patna");
   }
   public function offsetSet($offset, $value) {
      if (is_null($offset)) {
         $this->arr[] = $value;
      } else {
         $this->arr[$offset] = $value;
      }
   }
   public function offsetExists($offset) {
      eturn isset($this->arr[$offset]);
   }
   public function offsetUnset($offset) {
      unset($this->arr[$offset]);
   }
   public function offsetGet($offset) {
      return isset($this->arr[$offset]) ? $this->arr[$offset] : null;
   }
}
$obj = new myclass();
var_dump(isset($obj[0]));
var_dump($obj[0]);
unset($obj[0]);
var_dump(isset($obj[0]));
$obj[3] = "Pune";
var_dump($obj[3]);
$obj[4] = &#39;Chennai&#39;;
$obj[] = &#39;NewDelhi&#39;;
$obj[2] = &#39;Benguluru&#39;;
print_r($obj);
?>

Output

The above program displays the following output

bool(true)
string(6) "Mumbai"
bool(false)
string(4) "Pune"
myclass Object(
   [arr:myclass:private] => Array(
      [1] => Hyderabad
      [2] => Benguluru
      [3] => Pune
      [4] => Chennai
      [5] => NewDelhi
   )

)

The above is the detailed content of PHP ArrayAccess interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete