🐘 PHP Intermediate

What is the ArrayAccess interface in PHP?

Why Interviewers Ask This

Mid-level PHP roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.

Answer

The ArrayAccess interface allows objects to be accessed using array notation ($object["key"]). Implement four methods: offsetExists($offset) (called by isset($obj[$key])), offsetGet($offset) (called by $obj[$key]), offsetSet($offset, $value) (called by $obj[$key] = $val), and offsetUnset($offset) (called by unset($obj[$key])). This is used by Laravel's Collections, Symfony's ParameterBag, and many ORMs so their objects behave like arrays. It enables a uniform interface where consuming code does not need to know if it is working with a plain array or a complex object — both support the same [] access syntax.

Common Mistake

Many candidates answer correctly but can't explain the 'why'. Always be prepared to justify your answer with a concrete example or use case from your PHP experience.