13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Underst<strong>and</strong>ing Advanced Object-Oriented Functionality in <strong>PHP</strong><br />

189<br />

Listing 6.4<br />

Continued<br />

}<br />

function rewind()<br />

{<br />

$this->currentIndex = 0;<br />

}<br />

function valid()<br />

{<br />

return $this->currentIndex < $this->count;<br />

}<br />

function key()<br />

{<br />

return $this->currentIndex;<br />

}<br />

function current()<br />

{<br />

return $this->obj->data[$this->currentIndex];<br />

}<br />

function next()<br />

{<br />

$this->currentIndex++;<br />

}<br />

class Object implements IteratorAggregate<br />

{<br />

public $data = array();<br />

function __construct($in)<br />

{<br />

$this->data = $in;<br />

}<br />

function getIterator()<br />

{<br />

return new ObjectIterator($this);<br />

}<br />

}<br />

$myObject = new Object(array(2, 4, 6, 8, 10));<br />

$myIterator = $myObject->getIterator();<br />

for($myIterator->rewind(); $myIterator->valid(); $myIterator->next())<br />

{<br />

$key = $myIterator->key();<br />

$value = $myIterator->current();<br />

echo $key." => ".$value."";}<br />

?>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!