In our second article on the standard PHP library, David Fells explains the new Arrray object, introduces Iterators and the ArrayIterator, and discusses some practical examples of their usage. This article assumes a knowledge of how Exceptions work in PHP 5.
Programmers familiar with Java and .NET will be at home with using the new ArrayObject class. The ArrayObject class has a definition like this:
class ArrayObject implements IteratorAggregate, ArrayAccess, Countable {
public function __construct($array);
public function append($value);
public function count();
public function getArrayCopy();
public function getIterator();
public function offsetExists($index);
public function offsetGet($index);
public function offsetSet($index, $newval);
public function offsetUnset($index);
}
As you see in the declaration, the ArrayObject class implements three interfaces. The Countable interface requires the public method count() to be defined and allows the built in PHP count() function to be used on the object. The ArrayAccess interface is used to override array access of objects. For example, the offsetGet($index) method is used in place of the normal array style: $array[$index]. This interfaces calls for the public methods offsetExists(), offsetGet(), offsetSet() and offsetUnset(). The IteratorAggregate interface requires the public method getIterator(), which allows the calling code to use an external iterator for object traversal.
It is worth noting that the IteratorAggregate interface implements the Traversable abstract base interface, which is used to detect if a class is traversable using foreach. This interface must be implemented by either Iterator (an iterator object) or IteratorAggregate (an object to be iterated).
The first line creates the ArrayObject object and the next three lines store the values "a", "b" and "c'' in it. The next four lines demonstrate using the offsetGet() method and the count() method to retrieve values at a specified offset and to return the number of elements in the array, respectively. Next we unset the first index value by calling offsetUnset() and then change the value of the second index to "a". We then repeat the process of outputting the values in the array and the number of elements in the array. This script will output the following:
First Element: a
Second Element: b
Third Element: c
Number of Elements: 3
First Element:
Second Element: a
Third Element: c
Number of Elements: 2
Note that after calling unset, the index at position 0 still exists but has no value and is not included when calling count(). This seems like a counterintuitive behavior. It seems that the expected result would have been to completely remove the element - index and all - from the array and shift the other elements down. The term unset implies that after calling the method, the value is no longer set at all - meaning it is not set to NULL, 0, '' or anything else, and the index does not exist in the array. I would be interested to hear readers' thoughts on this, as I see it as poor implementation.