Home arrow PHP arrow Iterators in the Simplest Sense: Traversing Different Data Structures

Iterators in the Simplest Sense: Traversing Different Data Structures

Here we are again. Welcome to the second tutorial of the series “Iterators in the simplest sense.” Just in case you didn’t know, this series introduces Iterators in PHP 4 – PHP 5, explaining their basic concepts, and teaches you how to use them in practical projects, which can be utilized as part of larger PHP applications.

  1. Iterators in the Simplest Sense: Traversing Different Data Structures
  2. Extending the scope of iterators: building a file iterator class
  3. Building a concrete example: using the “FileIterator” class
  4. More iterators ahead: creating a MySQL result set iterator class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
March 15, 2006

print this article




As you’ll probably recall, in the first tutorial of this series, I briefly explained the theory behind Iterators in PHP, and highlighted some of their benefits by constructing an array iterator class. As you know, this array iterator was created as a subclass of the abstract “Iterator” class that I defined at the beginning of the first article, demonstrating that building an iterating programming structure in PHP is not only easy, but also fun and instructive.

Nevertheless, an array iterator class isn’t very useful when evaluated as a standalone structure, isolated from other programming modules that might exist within the same Web application. In accordance with this, the real power of such a structure is leveraged when used as a building block of larger programs, which can use at a lower level some (or all) the methods provided by the array iterator.

In order to illustrate how you can implement an array iterator within your own PHP applications, without sacrificing much of its functionality, over this second installment of the series, I’ll show you how different data structures can be traversed by the same set of abstract methods (called an interface) defined previously within the base abstract “Iterator” class. Following this approach, I’ll provide a common interface for iterating over distinct types of data structures, no matter if they’re only flat text files, arrays or MySQL result sets.

By the end of this tutorial, I hope you’ll have an accurate idea of how to use a generic Iterator structure. I will show how it is useful for traversing different data structures by providing specific implementations for each of the abstract methods defined within the base Iterator class. So, let’s not waste any more time and start building different iterators in PHP!

>>> 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: