Over a million developers have joined DZone.

ArrayAccess vs ArrayObject

DZone 's Guide to

ArrayAccess vs ArrayObject

· Web Dev Zone ·
Free Resource

I help people qualify for Zend Certification and in the last few months I've had questions about both ArrayAccess and ArrayObject. This post is an attempt to illuminate both.

In very simple terms, ArrayAccess is an interface, which you can implement in your own objects; ArrayObject, on the other hand, is a class, which you can either use or extend.


BasketArrayAccess is an interface built in to PHP which lets you dictate how PHP behaves when an object has array syntax (square brackets) attached to it. Interfaces are collections of function prototypes that we can use as templates for our own code. If you read the manual page for ArrayAccess, it shows four functions:

ArrayAccess {
/* Methods */
abstract public boolean offsetExists ( mixed $offset )
abstract public mixed offsetGet ( mixed $offset )
abstract public void offsetSet ( mixed $offset , mixed $value )
abstract public void offsetUnset ( mixed $offset )

To implement this interface, we just declare all these methods in our class. Here is an example class:

class PrettyBasket implements ArrayAccess
    protected $contents = array();
    public function offsetExists($index) {
        return isset($this->contents[$index]);
    public function offsetGet($index) {
        if($this->offsetExists($index)) {
            return $this->contents[$index];
        return false;
    public function offsetSet($index, $value) {
        if($index) {
            $this->contents[$index] = $value;
        } else {
            $this->contents[] = $value;
        return true;
    public function offsetUnset($index) {
        return true;
    public function getContents() {
        return $this->contents;

In addition to accessing the properties and methods of a Basket object, we can also use array notation with it, so we can write code along these lines.

$basket = new PrettyBasket();
$basket->madeOf = 'wicker';
$basket->capacity = '14';
$basket[] = 'pie';
$basket[] = 'apples';
$basket[] = 'map';
// $contents = $basket->getContents();
//echo $contents[0];
echo $basket[0];

The $basket variable is a Basket object, but we're using $basket[] notation with the object, so how does that work? In fact when we do this, PHP will check if we implement the ArrayAccess interface, and if we do then it calls our methods. This is very neat and can make for an easy way to give quick access to an array stored in one of the object properties, as we've done here. You can see that there's some code commented out and replaced by the one line following it - it's much shorter which means this is attractive for using to output data into templates. Another good example is to create behaviour such as SimpleXMLElement has, which uses this array notation to present the attributes of a tag.


ArrayObject is an object, you can implement and extend it as you usually would. It implements a bunch of useful interfaces and wraps them up into an object. For creating an object to look a lot like an array, take a look at ArrayObject! It implements ArrayAccess as we saw above, but also Countable, Traversable, and others.

The upshot is a class which, out of the box, can give us the behaviour shown above, plus foreach behaviour which foreaches the array elements rather than the object properties, support for serialising the data, and more. This is very useful when we work with this kind of pattern (typically data storage patterns but I'm open to hearing stories about how you use it in your own code).

You can extend ArrayAccess and create your own class, adding properties and methods in the usual way, and using the object exactly as you usually would ... except that it has all this array functionality built in as well! It's a powerful class but do bear in mind that although we can foreach it and access its individual elements, it won't look like an array to any of PHP's array functions. So for many applications, you do still want an array (or an object which can return you the array it's storing things in).

Hopefully that gives a clearer idea of what these Array* things are and when they'll be useful. Are you implementing these yourself? I'd love some more practical case studies if you want to share.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}