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

Building a concrete example: using the “FileIterator” class - PHP

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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In order to illustrate the functionality of the “FileIterator” class I built previously, first I’ll create a simple text file, populated with some basic data. Then I will instantiate an object from the corresponding class, so I’m able to use its methods. First of all, here’s the basic “test.txt” text file:

This is the data for line 1
This is the data for line 2
This is the data for line 3
This is the data for line 4
This is the data for line 5
This is the data for line 6
This is the data for line 7
This is the data for line 8
This is the data for line 9
This is the data for line 10

And next, you can see the PHP script that traverses the above text file:

// instantiate 'FileIterator' object
$fIterator=&new FileIterator('test.txt');
// display first line of file
echo $fIterator->reset();
// display current line of file
echo $fIterator->current();
// display next line of file
echo $fIterator->next();
// display final line of file
echo $fIterator->end();
// display previous line of file
echo $fIterator->prev();
// seek a line within file
echo $fIterator->seek(2);
// count number of lines in file
echo $fIterator->count();

The above snippet might seem like a trivial example, but in fact it’s demonstrating the ease of traversing a particular text file. Of course, here I populated the sample flat file with basic data, but think about the possible implementations for this “FileIterator” class, where file data has to be accessed either in a linear or random mode. Without a doubt, this class can be pretty helpful within a PHP application, particularly considering its ease and understandable source code.

Right, now you saw how simple it is to build a “FileIterator” class, by utilizing as a base class the pertinent array iterator you learned before. However, there are still more examples to show you, in order to continue demonstrating the versatility of iterators in PHP.

For this reason, in the next few lines I’ll define a result set iterator class, which comes in very handy for traversing MySQL data sets. Curious about how this is achieved? Fine, keep on reading to learn more.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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