_rowset = new Rowset($this); } public function process() { // this is the place to show how using an iterator, with foreach // See the CardGame.php file $this->_rowset->process(); } } class Rowset implements Iterator { protected $_currentRow; protected $_file; public function __construct($file) { $this->_file = $file; } /** * composite pattern: run through all rows and process them * * @return void */ public function process() { // this actually calls rewind(), { next(), valid(), key() and current() :} /** * THE key feature of the Iterator Pattern is to provide a *public contract* * to iterate on a collection without knowing how items are handled inside * the collection. It is not just an easy way to use "foreach" * * One cannot see the point of iterator pattern if you iterate on $this. * This example is unclear and mixed with some Composite pattern ideas. */ foreach ($this as $line => $row) { $row->process(); } } public function rewind() { // seek to first line from $this->_file } public function next() { // read the next line from $this->_file if (!$eof) { $data = ''; // get the line $this->_currentRow = new Row($data); } else { $this->_currentRow = null; } } public function current() { return $this->_currentRow; } public function valid() { return null !== $this->_currentRow; } public function key() { // you would want to increment this in next() or whatsoever return $this->_lineNumber; } } class Row { protected $_data; public function __construct($data) { $this->_data = $data; } public function process() { // do some fancy things here ... } }