Iterator (based on protocol method)

Intent
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Motivation
We want to be able to postpone the effects of a computation, for example delay printing from a function to webserver output until whole web-page is assembled
Implementation
This is an alternative implementation based on protocol method, rather than interfaces
Examples

class alt_iterator {
  private $st = array();

  function next() { ... }
  function current() { ... }
  function valid() { ... }
  //... put the rest here

  function protocol() {
    return array(
             array($this,'next'),
             array($this,'current'),
             array($this,'valid'),
	     .....
	     );
  }
  
}

//an example use
function do_them($itor, $callback ) {
  list($next, $current, $valid, ...) = $itor->protocol();
  while($valid) {
    $callback( $current );
    $next();
  }
}
Powered by Drupal, an open source content management system