PHP
  Home arrow PHP arrow Page 3 - Iterators in the Simplest Sense: An Accessible Implementation in PHP 4
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: An Accessible Implementation in PHP 4
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2006-03-08


    Table of Contents:
  • Iterators in the Simplest Sense: An Accessible Implementation in PHP 4
  • What is an Iterator? defining the core concepts
  • Building in an Iterator in PHP 4: setting up a functional example
  • Deriving subclasses from the base Iterator: building an array 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: An Accessible Implementation in PHP 4 - Building in an Iterator in PHP 4: setting up a functional example
    ( Page 3 of 4 )

    One of the best things about PHP is that once the subjacent theory has been understood, translating it into functional code is really simple. This is precisely the case with Iterators, which, as you’ll see soon, are actually simple to code. In order to supply an illustrative example of how to write a working Iterator in PHP 4, what I’ll do next is define an abstract “Iterator” class that has some of the abstract methods I discussed earlier, useful for iterating over a generic data structure. Here’s the source code for this class:

    // base abstract 'Iterator' class
    class Iterator{
        function Iterator(){
            if(get_class($this)=='Iterator'||!is_subclass_of
    ($this,'Iterator')){
                trigger_error('This class is abstract. It cannot be
    instantiated!',E_USER_ERROR);
            }
        }
        // abstract 'current()' method
        function current(){}
        // abstract 'prev()' method
        function prev(){}
        // abstract 'next()' method
        function next(){}
        // abstract 'end' method
        function end(){}
        // abstract 'reset()' method
        function reset(){}
        // abstract 'seek()' method
        function seek(){}
        // abstract 'count()' method
        function count(){}      
    }

    That’s it. I told you it was really easy! As you can see, I defined the above class in such a way that it exposes a few methods for traversing a data structure of any type. Also, I defined this class as abstract, by including a checking block inside the constructor. This prevents the class from being instantiated, either when directly called within client code, or when invoked from inside a non-subclass.

    Essentially, this sample “Iterator” class has seven different methods for iterating and processing data. It’s clear to see that each of them are extremely intuitive and understandable. Although none of these methods exposes a concrete implementation within the corresponding class, their names are suggestive enough to indicate what they’ll do when they're concretely defined.

    In accordance with the concepts I explained above, the “reset()”, “prev()”, “current()”, “next()” and “end()” methods should be defined within the pertinent subclasses, so they must provide an easy way to move back and forth across a particular data structure. In a similar fashion, the “seek()” and count()” method should be defined in order to allow the seeking of a specific element, and the counting of the number of elements that comprise the structure itself.

    Okay, at this point you know how a generic Iterator looks. Now, it’s time to do something useful with this powerful structure, so it can be used within a PHP application. Want to learn how to build an array Iterator, using the base class you just learned? 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek