Just stumbled across this by mistake, trying to write an iterator class of my own
interface Iterator {} // -> Fatal error: Cannot redeclare class iterator
Hmmm
$iterator = new Iterator();
print_r($m = get_class_methods($iterator));
Produces an object with 24 methods for iteration. It seems mainly aimed at directory iteration, with alot of file related methods. There is also an Iterator interface built-in,with 5 methods that lend themselves to any structure. Quite a useful find...there should be alot more documentation about this 🙂
<?php
class MySqlResultIterator implements Iterator {
protected $result;
private $collection;
private $pointer;
function __construct($collection){
$this->collection =& $collection;
}
function current(){
$this->result = mysql_fetch_array($this->collection, MYSQL_ASSOC);
}
function next(){
++$this->pointer;
mysql_data_seek($this->collection, $this->pointer);
}
function key(){
echo "Not implemented";
}
function valid(){
echo "Not implemented";
}
function rewind(){
mysql_data_seek($this->collection, 0);
}
}
$iterator = new MySqlResultIterator(mysql_result("SELECT foo FROM bar"));
$iterator->current();
$iterator->next();
?>
(untested)
Note: Just found this little lot in the manual