L'interface Iterator
Introduction
Interface pour les it�rateurs ou les objets externes qui peuvent �tre it�r�s eux-m�mes en interne.
Sommaire de l'Interface
Exemple #1 Exemple simple
Cet exemple montre l'ordre d'appel des m�thodes lors d'un appel � l'instruction foreach sur un it�rateur.
<?php
class myIterator implements Iterator {
private $position = 0;
private $array = array(
"premierelement",
"secondelement",
"dernierelement",
);
public function __construct() {
$this->position = 0;
}
function rewind() {
var_dump(__METHOD__);
$this->position = 0;
}
function current() {
var_dump(__METHOD__);
return $this->array[$this->position];
}
function key() {
var_dump(__METHOD__);
return $this->position;
}
function next() {
var_dump(__METHOD__);
++$this->position;
}
function valid() {
var_dump(__METHOD__);
return isset($this->array[$this->position]);
}
}
$it = new myIterator;
foreach($it as $key => $value) {
var_dump($key, $value);
echo "\n";
}
?>
L'exemple ci-dessus va afficher quelque chose de similaire � :
string(18) "myIterator::rewind" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" int(0) string(12) "premierelement" string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" int(1) string(13) "secondelement" string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" int(2) string(11) "dernierelement" string(16) "myIterator::next" string(17) "myIterator::valid"
Sommaire
- Iterator::current ? Retourne l'�l�ment courant
- Iterator::key ? Retourne la cl� de l'�l�ment courant
- Iterator::next ? Se d�place sur l'�l�ment suivant
- Iterator::rewind ? Replace l'it�rateur sur le premier �l�ment
- Iterator::valid ? V�rifie si la position courante est valide