Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Standard PHP Library and Classes

The ArrayAccess interface

Reference for the PHP ArrayAccess interface: use $obj[$key] syntax on your own classes with offsetGet, offsetSet, offsetExists and offsetUnset, plus edge cases.

ArrayAccess lets an object respond to square-bracket syntax. When a class implements its four methods, $obj['key'] calls offsetGet(), $obj['key'] = 1 calls offsetSet(), isset($obj['key']) calls offsetExists() and unset($obj['key']) calls offsetUnset().

It only changes the syntax. The object is still an object: is_array() is false, array functions such as array_keys() do not accept it, and foreach needs a separate Iterator or IteratorAggregate.

PHP
class Bag implements ArrayAccess
{
    private array $data = [];
    public function offsetExists(mixed $offset): bool { return isset($this->data[$offset]); }
    public function offsetGet(mixed $offset): mixed { return $this->data[$offset]; }
    public function offsetSet(mixed $offset, mixed $value): void { /* ... */ }
    public function offsetUnset(mixed $offset): void { unset($this->data[$offset]); }
}

Try it

PHP A settings object with array syntax

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP empty(), ?? and which methods they call

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
interface ArrayAccess
{
    public function offsetExists(mixed $offset): bool;
    public function offsetGet(mixed $offset): mixed;
    public function offsetSet(mixed $offset, mixed $value): void;
    public function offsetUnset(mixed $offset): void;
}
Name Description
offsetExists($offset) Called by isset($obj[$k]), and first by empty() and ??. Return true if the offset exists.
offsetGet($offset) Called when reading $obj[$k]. Return the value.
offsetSet($offset, $value) Called when writing $obj[$k] = $v. $offset is null for $obj[] = $v.
offsetUnset($offset) Called by unset($obj[$k]).

Return value: offsetExists() returns bool, offsetGet() returns any value, the other two return nothing.

Usage notes

  • isset() and ?? call offsetExists() only. isset($obj['a']) does not call offsetGet(), so a stored null can count as set if you return true from offsetExists().
  • empty() and ?? need both. They call offsetExists() first and offsetGet() only if it returned true, which the second example shows.
  • Push syntax passes null. $obj[] = $x calls offsetSet(null, $x); handle it or throw.
  • No indirect modification. $obj['list'][] = 1 calls offsetGet('list') and modifies a copy, producing a notice ("Indirect modification of overloaded element"). Return by reference (&offsetGet) or return objects to avoid it.
  • Keys are not converted. Unlike arrays, $obj['1'] passes the string '1' and $obj[true] passes true. Normalize inside the methods if you care.
  • Add return types. Since PHP 8.1 omitting them triggers a deprecation unless you add #[\ReturnTypeWillChange].
  • Not iterable or countable. Pair with Countable and IteratorAggregate for a full collection. ArrayObject already does all of this.