Home arrow PHP arrow Page 2 - Iterators in the Simplest Sense: An Accessible Implementation in PHP 4

What is an Iterator? defining the core concepts - PHP

In this first article of a series, Alejandro Gervasio explains the core concepts of Iterators in PHP, concentrating most of his efforts on the subject’s practical side.

  1. Iterators in the Simplest Sense: An Accessible Implementation in PHP 4
  2. What is an Iterator? defining the core concepts
  3. Building in an Iterator in PHP 4: setting up a functional example
  4. Deriving subclasses from the base Iterator: building an array Iterator class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
March 08, 2006

print this article



Definitely, I’m pretty sure that you’ve heard many times before the term “Iterator”…but, what is it in fact? Essentially, when you’re using an Iterator, what you’re actually doing is creating the necessary programming logic (usually encapsulated inside a class) that will allow you to traverse any data structure by using a common set of methods (this is called an interface too). As I said before, data can be of any type, so whether you’re using arrays, objects, files, directories, or database result sets, an Interator should expose the corresponding methods for iterating over the data structure in question, and manipulating each one of its visible properties.

To clarify the concepts I deployed before, here is a typical signature for an Iterator class in PHP 4, which exposes some of the most common generic methods for traversing a generic data structure:

class MyIterator{
    // constructor
    public function MyIterator($data){
    // method for fetching previous element
    public function previous(){
    // method for fetching current element
    public function current(){
    // method for seeking a particular element
    public function seek(){
    // method for fetching next element
    public function next(){
    // method for checking if current element is valid
    public function valid(){

As the above example shows, I defined a generic “MyIterator” class, which exposes a few abstract methods for traversing a generic data structure. If you pay attention to the above class, you’ll realize that it has some intuitive methods for moving back and forward across the structure, such as the “previous()”, “current()” and “next()” methods respectively, and other useful ones, like the “seek()” method, which comes in very handy for navigating to a specified position within the structure. Finally, the “valid()” method should be used for determining whether the current element within the data structure is valid or not.

Of course, eventually I might attach more methods for the above class, but for the moment, the example you just saw should give you a basic idea of how an Iterator works. Asides from the existence of the corresponding class methods, it’s worth nothing that this class isn’t referencing any data structure in particular. All the declared methods are entirely generic and don’t provide a concrete implementation in each case, which is very convenient for achieving a good level of abstraction.

From this point onward, and after having defined the base “MyIterator” class, it’s possible (and certainly recommended) to derive any number of subclasses, which will implement specifically each pertinent method, in order to traverse a specific data structure, that is arrays, objects, files, and so on.

So, that’s a little bit of theory. Now, come with me and read the next section, in order to see how an Iterator can be traduced to real PHP code, so you can start using it in your applications.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: