PHP
  Home arrow PHP arrow Page 3 - Iterators in the Simplest Sense: Traversing Data Structures in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Iterators in the Simplest Sense: Traversing Data Structures in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek