A Closer Look At ArrayObject
Out Of Date Warning
Languages change. Perspectives are different. Ideas move on. This article was published on April 26, 2010 which is more than two years ago. It may be out of date. You should verify that technical information in this article is still current before relying upon it for your own purposes.
Every once in a while I discover a really cool, really underutilized component in PHP and I just have to write about it. I recently discovered (through my use of Zend Framework) the implementation of the ArrayObject class.
ArrayObject is an object that is designed to behave exactly like an array. If that seems confusing, don’t worry, it’s not. ArrayObject is an object. It follows all the rules of how objects work. But it’s designed to implicitly behave like an array for all intents and purposes, including being used in a foreach loop, and accessing it’s properties just like you would access the values in an array. Consider the following code sample:
<?php $array = array('one', 'two', 'three'); $arrayObj = new ArrayObject($array); var_dump(($array == $arrayObj)); // Outputs true. ?>
This might not seem too terribly exciting; in fact, any of the SPL classes that implement ArrayAccess and IteratorAggregate will exhibit this behavior. You can typically access an object’s properties with a foreach loop. But the key here is the fact that ArrayObject gives you the flexibility of an array along with the power of an object in PHP – something that makes it very interesting.
The way objects are used in PHP 5, assigning them doesn’t copy them; it copies their location in a lookup table for later retrieval. This means that an object modified in one area of the application reflects those changes everywhere it has been passed; there is no need to do any extra work to ensure that code using your objects has those changes. Arrays, on the other hand, are copied on assignment, meaning that changes made in one component of the application will not be reflected in the original array, unless you passed the array by reference. But because ArrayObject behaves like an array while still following the rules of an object, it has a number of distinct advantages.
It’s most obvious advantage is the ability to reflect changes throughout an application, avoiding the need for copying an array, modifying it, and then returning it for reassignment, or the ever-dreaded passing by reference option. And the ArrayObject, part of the SPL, offers some performance improvements over conventional coding options. Finally, the ArrayObject allows developers to be more purist in their object-oriented implementations will still gaining the flexibility and structure of arrays.
This object is not without it’s drawbacks however: there are a number of features that cannot be used with this object, including most of the array_* functions in the PHP library. This can be a significant drawback to those skilled with the use of arrays, though developers can extend the class and implement some of this behavior themselves.
Still, ArrayObject is being used in lots of PHP applications, including Zend Framework (Zend_Registry extends ArrayObject, for example). It’s a great SPL component, and one I’ll be making more more use of in the future.
Learning design patterns doesn't have to suck.
Design patterns open a whole new world of possibilities. So why are you avoiding them? This brand new book will help you finally understand these wonderful programming techiques!Learn design patterns TODAY »