PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Advanced OOP With SPL In PHP 5

PHP 5 Interfaces

With the innovations offered by PHP5 and SPL, you can now use advanced OOP concepts like classes, interfaces and, thanks to SPL, indexers and iterators, in a way that was never possible before.

SPL, short for the Standard PHP Library is an extension to php that adds native interfaces, classes and functions to the PHP core. These extensions are, at the time of writing, not properly documented, so I will try to explain the usage of the three SPL interfaces we will need to complete this project.

The ArrayAccess interface

This interface, when implemented allows for indexers (the use of the array index operators []) to be used on an arbitrary object.

In PHP all indexed objects need four methods to be treated as an array and must declare themselves to implement the ArrayAccess interface.

  • offsetExists($offset)
    This method is used to tell php if there is a value for the key specified by offset. It should return true or false.
  • offsetGet($offset)
    This method is used to return the value specified by the key offset.
  • offsetSet($offset, $value)
    This method is used to set a value within the object, you can throw an exception from this function for a read-only collection.
  • offsetUnset($offset)
    This method is used when a value is removed from an array either through unset() or assigning the key a value of null. In the case of numerical arrays, this offset should not be deleted and the array should not be reindexed unless that is specifically the behavior you want.

The Countable interface

This interface hooks the global count() function so that the size of your object is correctly determined from this function. This interface relies on PHP 5.1.

To implement Countable you must provide a public class method called count() that returns an integer and mark the class as implementing Countable.

The Iterator interface

Iterators are an extremely useful part of SPL; they allow you to use your objects in loops. You might be wondering why Iterator is not implied by ArrayAccess and the reason is that it does not provide a mechanism to tell PHP about the possible keys and their proper order. To implement a foreach loop, this is very important. To implement Iterator you need to write five methods.

  • current()
    This method returns the current index’s value. You are solely responsible for tracking what the current index is as the interface does not do this for you.
  • key()
    This method returns the value of the current index’s key. For foreach loops this is extremely important so that the key value can be populated.
  • next()
    This method moves the internal index forward one entry.
  • rewind()
    This method should reset the internal index to the first element.
  • valid()
    This method should return true or false if there is a current element. It is called after rewind() or next().

It is worth mentioning that there are many types of iterators offered by SPL, but for this example we do not need a more specialized iterator and will only implement Iterator.

In This Article