HomePHP 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.
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.