PHP
  Home arrow PHP arrow Page 3 - Iterators in the Simplest Sense: Trave...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Iterators in the Simplest Sense: Traversing Data Structures in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2006-03-22

    Table of Contents:
  • Iterators in the Simplest Sense: Traversing Data Structures in PHP 5
  • Working with iterators in PHP 5: using some predefined SPL classes
  • Traversing text files the easy way: the brand new FileIterator class
  • Traversing database result sets: building a MySQL iterator class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Iterators in the Simplest Sense: Traversing Data Structures in PHP 5 - Traversing text files the easy way: the brand new FileIterator class


    (Page 3 of 4 )

    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.

    More PHP Articles
    More By Alejandro Gervasio


       · This last tutorial of the series shows how to apply the Iterator pattern in PHP 5,...
       · The idea is great! I couldn't get it to work, however. Seems like the problem lies...
       · Thank you for reading and commenting on this PHP article. With reference to the...
     

       

    PHP ARTICLES

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT