Iterators


Iterators are another form of object overloading similar to the concept of __toString() for strings discussed earlier in the chapter. However, instead of overloading an object to behave like a string, iterators allow you to treat objects as arrays that are traversable using the foreach construct. There are a number of ways to implement iteration in your classes; the easiest is by treating an instance of an object as an array. When this is done, all of the object's public properties can be traversed as an associative array using the foreach statement as shown in Listing 13.28:

Listing 13.28. Implementing the traversable Iterator
 <?php     class IterateExample {         public $var_one = 1;         public $var_two = 2;         private $var_three = 3;         public $var_four = 4;     }     $inst = new IterateExample();     foreach($inst as $key => $value) {         echo "$key = $value\n";     } ?> 

To implement a more complex form of iterators in your class (for instance, one that has little to do with the properties in the class itself, such as database rows), three internal PHP interfaces must be introduced: traversable, Iterator, and IteratorAggregate. A representation of these interfaces as PHP code is found in Listing 13.29:

Listing 13.29. The Iterator Interfaces
 <?php      interface Traversable {           /* Empty interface */      }      interface IteratorAggregate {           public function getIterator();      }      interface Iterator {           public function rewind();           public function hasMore();           public function key();           public function current();           public function next();      } ?> 

The first of these interfaces, the TRaversable interface, is an empty interface used only to indicate whether a class can be traversed using the Iterator interface. Thus, to create a class that supports iteration, implement both the traversable and Iterator interfaces as shown in Listing 13.30.

Listing 13.30. Implementing the Iterator Interface
 <?php         class IterateExample implements  Iterator, Traversable {                 private $words = "The Quick Brown Fox";                 private $cur = 0;                 private $max = 0;                 function __construct() {                         $this->max = count(explode(" ", $this->words));                 }                 public function rewind() {                         $this->cur = 0;                 }                 public function hasMore() {                         if($this->cur < $this->max) {                                 return true;                         }                         return false;                 }                 public function key() {                         return $this->cur;                 }                 public function current() {                         $ar = explode(" ", $this->words);                         return $ar[$this->cur];                 }                 public function next() {                         $this->cur++;                 }         }         $inst = new IterateExample();         foreach($inst as $key => $value) {                 echo "$key = $value\n";         } ?> 

When Listing 13.30 is executed, the result is an object that iterates through the words in the phrase in the $words property of the object:

 0 = The 1 = Quick 2 = Brown 3 = Fox 



PHP 5 Unleashed
PHP 5 Unleashed
ISBN: 067232511X
EAN: 2147483647
Year: 2004
Pages: 257

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net