class ObjectIterator implements Iterator {
private $obj;
private $count;
private $currentIndex;
function __construct($obj)
{
$this->obj = $obj;
$this->count = count($this->obj->data);
}
function rewind()
{
$this->currentIndex = 0;
}
function valid()
{
return $this->currentIndex < $this->count;
}
function key()
{
return $this->currentIndex;
}
function current()
{
return $this->obj->data[$this->currentIndex];
}
function next()
{
$this->currentIndex++;
}
}
class Object1 implements IteratorAggregate
{
public $data = array();
function __construct($in)
{
$this->data = $in;
}
function getIterator()
{
return new ObjectIterator($this);
}
}
$myObject = new Object1(array(2, 4, 6, 8, 10));
$myIterator = $myObject->getIterator();
for($myIterator->rewind(); $myIterator->valid(); $myIterator->next())
{
$key = $myIterator->key();
$value = $myIterator->current();
echo $key." => ".$value."<br />";
}
Deprecated: Return type of ObjectIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 25
Deprecated: Return type of ObjectIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 29
Deprecated: Return type of ObjectIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 21
Deprecated: Return type of ObjectIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 17
Deprecated: Return type of ObjectIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 13
Deprecated: Return type of Object1::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 44
0 => 2
1 => 4
2 => 6
3 => 8
4 => 10