HomePHP Page 3 - Iterators in the Simplest Sense: Traversing Data Structures in PHP 5
Traversing text files the easy way: the brand new FileIterator class - PHP
Welcome to the final part of the series “Iterators in the Simplest Sense.” In this set of articles, you’ll learn the basic concepts of iterators in PHP 4 and PHP 5. The overall learning experience is strongly focused on the practical sense of the topic, so you can start quickly using iterators within your own PHP applications.
In order to demonstrate the functionality of the "FileIterator" class that I defined before, I'll first create a basic text file, which will be inputted right into the constructor, and second spawn an object from this class. In turn, all the respective methods will be called up, providing an easy method for navigating back and forward across the data file. Please look at the following basic sample text file:
This is line 1 of the data file that is being traversed by the FileIterator class. This is line 2 of the data file that is being traversed by the FileIterator class. This is line 3 of the data file that is being traversed by the FileIterator class. This is line 4 of the data file that is being traversed by the FileIterator class. This is line 5 of the data file that is being traversed by the FileIterator class. This is line 6 of the data file that is being traversed by the FileIterator class. This is line 7 of the data file that is being traversed by the FileIterator class. This is line 8 of the data file that is being traversed by the FileIterator class. This is line 9 of the data file that is being traversed by the FileIterator class. This is line 10 of the data file that is being traversed by the FileIterator class.
And next, analyze the PHP script that traverses the above file:
try{ $fIterator=new FileIterator('test.txt'); // reset pointer to beginning of file $fIterator->rewind(); // display current line of file echo $fIterator->current(); // move to next line of file $fIterator->next(); // display current line of filet echo $fIterator->current(); // display number of lines in file echo $fIterator->count(); // move file pointer to third line $fIterator->seek(3); // display third line echo $fIterator->current(); } catch(Exception $e){ echo $e->getMessage(); exit(); }
As the above example illustrates, a new instance of the "FileIterator" class is created, and then all its methods are called in sequence, in order to iterate over the "test.txt" file you just saw. Notice the ease of fetching, seeking and counting file data respectively, once the iterator is available inside the script. In this case I enclosed the whole sample code within a regular "try-catch" block, but more complex settings can be used to handle potential exceptions.
At this stage, hopefully you realize how simple it is to create a file iterator class in PHP 5. This example should be a good starting point, just in case you might want to experiment with some other preexisting classes included in the SPL package. Here, I only used the "ArrayObject" class, and it helped me to skip the annoying task of repeatedly defining the same class methods.
After demonstrating a functional implementation of the "FileIterator" class, a good corollary for this section would be building another iterating class, which can be utilized for traversing MySQL result sets. Bearing in mind this idea, in the next few lines, I'll create such a class, to help you understand how database records can be handled by a data traversing class. Go ahead and read the next section.