$this->key = 0; } public function current() { return $this->data[$this->key]; } public function key() { return $this->key; } public function next() { $this->key++; return true; } public function valid() { return isset($this->data[$this->key]); } } $iterator = new BasicIterator(); $iterator->rewind(); do { $key = $iterator->key(); $value = $iterator->current(); echo $key . ': ' . $value . PHP_EOL; } while ($iterator->next() && $iterator->valid()); $iterator = new BasicIterator(); foreach ($iterator as $key => $value) { echo $key . ': ' . $value . PHP_EOL; }
{ $this->_key = 0; } public function current() { return $this->_list[$this->_key]; } public function key() { return $this->_key; } public function next() { $this->_key++; return true; } public function valid() { return isset($this->_list[$this->_key]); } } $iterator = new BasicIterator($students); $iterator->rewind(); do { $key = $iterator->key(); $student = $iterator->current(); echo "{$key} : {$student}\n"; } while ($iterator->next() && $iterator->valid()); foreach ($iterator as $key => $student) { echo "{$key} : {$student}\n"; }