When Objects Act Like Arrays

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.


Netbeans 6.9 Beta + Zend Framework

Having just got my brand new MacBook Pro, I’ve been setting it up as a development environment (blog post about that to come). I decided to install the new Netbeans 6.9 beta. The main reason for this is the Zend Framework (and Symfony) support.

In the past I have found Netbeans to be pretty good with code-completion when being used with Zend Framework, however with the release of Zend Tool (something I do really like), you’ve had to switch from Netbeans to the command line in order to create the project and then create a new Netbeans project from existing sources. This was a bit of a hassle.

Now, all you need to do is download the Framework, go into the Netbeans preferences > PHP > Zend tab, Zend script box should point to the zf script (from within the bin directory of the ZF downloading). On Mac and Linux it wants the zf.sh file (on Windows it will probably want the zf.bat file, although not tested). Once that has been set up, you can now create brand new Zend Framework projects from within Netbeans, and it preconfigures everything for you. Lovely!

RIP Internet Explorer 6

The time I never thought I’d see is now on the horizon. The web moving away from supporting Internet Explorer 6.

Many web developers are all too aware of the pain of getting their websites working correctly in all web browsers and THEN having to make sure they work in IE6. This is not only inconvenient and irritating, but expensive. However, with Google announcing it is no longer supporting IE6 and now Amazon following their lead, it is appearing that very soon (not immediately) people will be forced to update/change their web browser to use these large and prominent websites. Many other sites have also dropped IE6 support (list at http://idroppedie6.com/).

Finally we will be able to spend our time on functionality rather than legacy support.