I’m a big advocate of object-oriented programming, even though only a few years ago I hated it and thought it was “over engineering”. I don’t believe, however, that object orientation is suitable for every circumstance. Anyway, that’s for another blog post. Objects are great, but not always the best when handling data, particularly things like data collections.
One of the nice things about storing collections of data in arrays rather than objects is the fact that you’re able to easily loop through the collection, and extract data from the collection depending on the position. Sometimes though we may want to manipulate the collection, and store more information than just the data in the collection. This is where SPL comes in.
As of version 5.0 PHP has included the SPL (Standard PHP Library). I am going to look at a couple of interfaces from this library in this blog post: ArrayAccess and SeekableIterator. These interfaces allow us to do a couple of things: SeekableIterator allows us to iterate through the object (eg. a foreach() loop), and the ArrayAccess allows us to extract data from the object as if it was an array. All this in a way that is completely controlled by the programmer.
Definition for the SeekableIterator:
SeekableIterator extends Iterator { /* Methods */ abstract public void seek ( int $position ) /* Inherited methods */ abstract public mixed Iterator::current ( void ) abstract public scalar Iterator::key ( void ) abstract public void Iterator::next ( void ) abstract public void Iterator::rewind ( void ) abstract public boolean Iterator::valid ( void ) }
Definition for the ArrayAccess:
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 ) }
The way these work is that they require a variety of methods to appear in the class. With SeekableIterator there is generally a “position” property which is just an integer holding the current position in the collection (so normally defaults to 0). The methods that are required are: seek, current, key, next, rewind and valid. The “seek” moves the position to the number passed in as an argument, “next” method normally increments the position by one, and “rewind” resets the position back to 0. “current” returns the item at which the position property is at, “key” tells you the current value of position and “valid” checks to make sure an item exists at that position. You can then put the object in a foreach loop as if it was an array!
foreach($iterableObject as $item { ... }
ArrayAccess requires fewer methods, and some of them are similar: “offsetExists” is the same as “valid” but rather than getting the position from the internal position property, it is passed in as a parameter. “offsetGet” returns the item at a given position and “offsetSet” sets an item to a given value. You can then treat an object like an array by using standard array notation:
$item = $arrayAccessObject[0];
A single class can implement both of these interfaces without conflict, and SPL contains many more things like this. I have not written this as a tutorial as the PHP Docs are incredibly good on this subject and easy to follow.
http://php.net/manual/en/class.seekableiterator.php
http://php.net/manual/en/class.arrayaccess.php
Great little insight in to the exceptionally useful SPL Interfaces.
I really like how implementing ArrayAccess and a flavour of one of the SPL Iterators (such as the SeekableIterator you have described) allow developers to encapsulate business logic which works upon a collection of data inside the object itself.
$cart = new ShoppingCart();
echo $cart[0]->getPrice()
foreach ($cart as $product) {
echo $product->getPrice();
}
echo $cart->getTotal();
The getTotal() method works on the internal array that ArrayAccess exposes, and yet the object can be easily accessed and iterated as a native PHP array.
The major benefit of working with objects which can be accessed in this manner lies in the simplicity of calling / consumer code. There is no need to provide additional methods to provide access to the internal data structure of an object, i.e. $cart->getItem(0); $cart->setItem(0, $product); You can simply use $cart[0] = new Product(); or $cart[] = new Product();
Thanks for the post!