Interfaz Iteradores
PHP5 también incluye tres interfaces, "Traversable", "Iterator" e "IteratorAgregate", las cuales implementan el patrón de iteración de la siguiente figura:
Veamos un ejemplo de uso del interfaz Iterator:
<?php
class ClaseIterable implements Iterator {
private $var;
public function __construct($array) {
assert(is_array($array) === true);
$this->var = $array;
}
public function rewind() {
reset($this->var);
}
public function current() {
return current($this->var);
}
public function key() {
return key($this->var);
}
public function next() {
return next($this->var);
}
public function valid() {
return ($this->current() !== false);
}
}
$objIterable = new ClaseIterable(array('Dato1','Dato2','Dato3'));
foreach ($objIterable as $indice => $valor)
echo "$indice: $valor<br>";
?>
Podemos encontrar más información sobre estos interfaces y otros incluidos en la Standard PHP Library (SPL) en la siguiente URL: http://www.php.net/helly/php/ext/spl