PHP
  Home arrow PHP arrow Page 3 - Iterators in the Simplest Sense: Traversing Different Data Structures
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 Different Data Structures
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2006-03-15


    Table of Contents:
  • Iterators in the Simplest Sense: Traversing Different Data Structures
  • Extending the scope of iterators: building a file iterator class
  • Building a concrete example: using the “FileIterator” class
  • More iterators ahead: creating a MySQL result set 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 Different Data Structures - Building a concrete example: using the “FileIterator” class
    ( Page 3 of 4 )

    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
     

       

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek