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.
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
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.
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
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??calloffsetExists()only.isset($obj['a'])does not calloffsetGet(), so a storednullcan count as set if you returntruefromoffsetExists().empty()and??need both. They calloffsetExists()first andoffsetGet()only if it returnedtrue, which the second example shows.- Push syntax passes
null.$obj[] = $xcallsoffsetSet(null, $x); handle it or throw. - No indirect modification.
$obj['list'][] = 1callsoffsetGet('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]passestrue. 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
CountableandIteratorAggregatefor a full collection.ArrayObjectalready does all of this.