PHP
  Home arrow PHP arrow Page 2 - 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? 
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 - What is an Iterator? defining the core concepts
    ( Page 2 of 4 )

    Definitely, I’m pretty sure that you’ve heard many times before the term “Iterator”…but, what is it in fact? Essentially, when you’re using an Iterator, what you’re actually doing is creating the necessary programming logic (usually encapsulated inside a class) that will allow you to traverse any data structure by using a common set of methods (this is called an interface too). As I said before, data can be of any type, so whether you’re using arrays, objects, files, directories, or database result sets, an Interator should expose the corresponding methods for iterating over the data structure in question, and manipulating each one of its visible properties.

    To clarify the concepts I deployed before, here is a typical signature for an Iterator class in PHP 4, which exposes some of the most common generic methods for traversing a generic data structure:

    class MyIterator{
        // constructor
        public function MyIterator($data){
        }
        // method for fetching previous element
        public function previous(){
        }
        // method for fetching current element
        public function current(){
        }
        // method for seeking a particular element
        public function seek(){
        }
        // method for fetching next element
        public function next(){
        }
        // method for checking if current element is valid
        public function valid(){
        }
    }

    As the above example shows, I defined a generic “MyIterator” class, which exposes a few abstract methods for traversing a generic data structure. If you pay attention to the above class, you’ll realize that it has some intuitive methods for moving back and forward across the structure, such as the “previous()”, “current()” and “next()” methods respectively, and other useful ones, like the “seek()” method, which comes in very handy for navigating to a specified position within the structure. Finally, the “valid()” method should be used for determining whether the current element within the data structure is valid or not.

    Of course, eventually I might attach more methods for the above class, but for the moment, the example you just saw should give you a basic idea of how an Iterator works. Asides from the existence of the corresponding class methods, it’s worth nothing that this class isn’t referencing any data structure in particular. All the declared methods are entirely generic and don’t provide a concrete implementation in each case, which is very convenient for achieving a good level of abstraction.

    From this point onward, and after having defined the base “MyIterator” class, it’s possible (and certainly recommended) to derive any number of subclasses, which will implement specifically each pertinent method, in order to traverse a specific data structure, that is arrays, objects, files, and so on.

    So, that’s a little bit of theory. Now, come with me and read the next section, in order to see how an Iterator can be traduced to real PHP code, so you can start using it in your applications.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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