menu_book Course Curriculum (PHP) expand_more
ArrayAccess
Learn ArrayAccess in PHP with a focused explanation, a dedicated runnable example, and a relevant engineering boundary.
ArrayAccess is an atomic PHP topic in Modern PHP Patterns and Testing. This lesson isolates the construct so you can see what it changes, what value or state it produces, and which surrounding rules matter.
The example below is written specifically for ArrayAccess. Run it once as shown, trace the observable result, then modify the relevant operand, argument, input, or state. Do not replace the construct with a neighboring feature: the purpose is to learn this exact behavior in context.
Topic-specific example
<?php
class Bag implements ArrayAccess { private array $d=[]; public function offsetExists($o):bool{return isset($this->d[$o]);} public function offsetGet($o):mixed{return $this->d[$o]??null;} public function offsetSet($o,$v):void{$this->d[$o]=$v;} public function offsetUnset($o):void{unset($this->d[$o]);} }
$b=new Bag(); $b["theme"]="dark"; echo $b["theme"];
With ArrayAccess, watch mutation, ordering, duplicate handling, and whether the operation returns a new value or changes the existing collection.