View Full Version : How does Iterator work?

12-08-2008, 12:32 AM
I was looking here:
and I saw: Iterator $itorStack. What is it/what does it do?

12-08-2008, 01:31 AM
I can tell you it is for object-orientated program, but that's all I know. I don't know how to explain it, but the PHP manual has a section for it.

12-08-2008, 05:54 AM
Iterator is an access interface which defines how an object should be handled with the use of foreach and other looping mechanisms. The one that bugs me is that foreach works on an array, but an array is primitive meaning its not really of type iterable.

In this specific example, it typehints PHP to require a typeof Iterator for use as an argument when constructing the object of type Pagination.

Interfaces are one of the most common items overlooked new OO programmers. It develops a contract that can force objects to be used in certain circumstances. A very common interface in most languages (not PHP, lol) is a Comparator or Comparable iterator of some sorts. This contracts an object to have a method to compare to another object similar to itself, and return a result in a numerical fashion (+, 0, -) and is generally useful in sorting algorithms. Once an object is known to be of type Comparable, we know that we can invoke a method compareTo (or similar name) on it. For this reason, I really hope that PHP develops generics in the near future (so I can typecast a typeof Comparable as a requirement and implement Comparable<MyObject> on a class).

You'll notice two other interfaces used in my pagination object, one is Countable, and the other is SeekableIterator. Countable is used to list the number of items in a collection (with a count($list) or $list->count() fashion), and seekable lets use choose a specific location within a collection. You'll also notice that they are not theoretically required either, as both counting and seeking can be performed on an iterable object without the use of either count or seek but by use of iteration instead.

I actually mention in I think its the second post in that thread that I thought I was being sneaky with using Iterable, but it ended up causing more trouble than it was worth. Came out good in the long run though :thumbsup:

12-08-2008, 07:35 AM
Ok, now I'm more confused.
So can you show me an example of how the iterator $itorStack works and why you used instanceof Countable and instanceof SeekableIterator?

12-08-2008, 11:40 AM
Ok, now I'm more confused.
So can you show me an example of how the iterator $itorStack works and why you used instanceof Countable and instanceof SeekableIterator?


$aValues = array('Cat', 'Dog', 'm' => 'Mouse'); // Arrays are for some reason iterable, but are not of type iterator
$itorStack = new ArrayObject($aValues); // ArrayObject is of type Iterator

foreach ($itorStack AS $stackKey => $stackVal)

Simple as that. Since $itorStack is an object of type Iterator, I can use it to break apart key/value combinations from the object using foreach. What the values are depend on what the Iterator key() and current() methods return. This is the purpose of iterator. I'm guessing its supposed to work with current, next, rewind etc:

while ($cur = current($itorStack))
echo $cur;

but it does not work correctly once you've started adding multiple keys and dimensions. So I'm thinking thats a bug.

instanceof keyword simply returns a boolean that confirms and object is of type x (except in PHP5.0, where it constructed a new object of type x and compared their signatures. Stupid).
Countable and seekableIterator are used to shortcut the functionality of the methods. With the use of countable, you don't need to iterate every object to count how many are available. You could override the countable method in any given collection object to return a number representing itself. This could be hard coded depending on functionality. It would be exploited if you wanted to extend the MySQLPagination to create the LimitedMySQL Pagination (since the count doesn't reflect the collection).
Seekable is the same idea. We provide an item we want to seek to (like the fourth) and we forward the record pointer to the fourth value. Otherwise we'll need to seek one by one to find the fourth record.

12-08-2008, 05:16 PM
You may also want to take a look at the SPL documentation.


It lists all the methods of the Iterator classes/interfaces.

12-09-2008, 06:56 AM
Iterators are used on the vector arrays on most of the languages. With the help of Iterators you can expand the objects functionality on PHP. Build an Interface first with what you want to declare,

eg; <?php // $array = ?. // initialize the array
$iterator = new Iterator($array);

After declaring an interface, you can implement the declarad interface.

Object Implement

eg; <?php
class ObjectIterator extends Iterator
var $_objectName;

The main advantage of this (iterator)function is, it wont make your application go slow and is easy for future developments.