PHP ArrayAcccess interface

In PHP, the ArrayAccess interface allows objects to be accessed like arrays using square bracket notation. When a class implements this interface, you can use $obj['key'] syntax to interact with the object's internal data without exposing the underlying array property directly.

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
}

Required 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

Example with Associative Array

Here's how to implement ArrayAccess with an associative array as the internal property ?

<?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);
?>
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
        )

)

Example with Indexed Array

The ArrayAccess interface also works with indexed arrays where numeric indices act as offsets ?

<?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) {
        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[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);
?>
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
        )

)

Conclusion

The ArrayAccess interface provides a clean way to make objects behave like arrays. By implementing the four required methods, you can control how data is accessed, modified, and checked within your objects while keeping the internal array structure private.

Updated on: 2026-03-15T09:24:33+05:30

380 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements