Home arrow PHP arrow Page 3 - Implementing the Iterator SPL Interface

Implementing the remaining methods of the Iterator interface - PHP

In this second part of a series, I make the MySQLi_ResultWrapper class a full implementer of the Iterator SPL interface. Implementing the rest of the methods declared by the interface is a straightforward process that allows us to traverse MySQL result sets by using a plain “foreach” construct.

TABLE OF CONTENTS:
  1. Implementing the Iterator SPL Interface
  2. Review: partially implementing partially the Iterator SPL interface
  3. Implementing the remaining methods of the Iterator interface
  4. The full source code of the MySQLi_ResultWrapper class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 18, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said in the previous segment, it’s mandatory to implement within the “MySQLi_ResultWrapper” class the rest of the methods declared by the Iterator interface. In reality, this process is pretty straightforward, and the code fragment below will hopefully demonstrate my point:

// get current result set pointer (implementation required by 'key()' method in Iterator interface)

public function key()

{

    return $this->_pointer;

}

 

 

// move forward result set pointer (implementation required by 'next()' method in Iterator interface)

public function next()

{

    ++$this->_pointer;

    $this->movePointer($this->_pointer);

    return $this;

}

 

 

// determine if result set pointer is valid or not (implementation required by 'valid()' method in Iterator interface)

public function valid()

{

    return $this->_pointer < $this->num_rows;

}

 

Didn’t I tell you that the implementation of the remaining methods declared by the Iterator interface was going to be easy to understand? Well, the code sample above should reaffirm this concept, since now those methods give the “MySQLi_ResultWrapper” class an API that permits us to traverse database result sets as if they were regular arrays, aside from allowing the internal pointer to move back and forth.

So far, so good. Now that the methods have a shiny new implementation, it’s time to show the full source code of the “MySQLi_ResultWrapper” class, so you can see how it looks after adding the methods to it.

This will be done in the following segment, so click on the link shown below and read the following lines. 



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: