PHP ArrayAcccess interface

PHPServer Side ProgrammingProgramming

Introduction

In PHP, ArrayAccess interface is used to develop a class that provides array like access to one of properties which is an array. Such array property may be manipulated without exposing it during object creation. ArrayAccess interface defines 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
}

Methods

ArrayAccess::offsetExists − Whether an offset exists

ArrayAccess::offsetGet − Offset to retrieve

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

ArrayAccess::offsetUnset − Unset an offset.

ArrayAccess Example

In following example, an associative array is internal private proprty of myclass. The key acts as offset. We can set, retieve and unset an item in the array. If offset is not given, it is treated as an integer, every time incrementing to next index.

Example

 Live Demo

<?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"] = 'Tamilnadu';
$obj[] = 'New State';
$obj["Hyderabad"] = 'Telangana';
print_r($obj);
?>

Output

Above program shows 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 property of class can also be an indexed array. In that case, index of element (starting from 0) acts as offset. When calling offsetSet(0 method without offset argument, index of array is incremented to 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] = 'Chennai';
$obj[] = 'NewDelhi';
$obj[2] = 'Benguluru';
print_r($obj);
?>

Output

Above program shows 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
   )

)
raja
Published on 21-Sep-2020 15:12:01
Advertisements