<?php
/**
* Collection that wraps a numeric array.
* All five public methods are needed to implement
* the Iterator interface.
*/
class Collection implements Iterator
{
private $_content;
public $_index = 0;
public function __construct(array $content)
{
$this->_content = $content;
}
public function rewind()
{
$this->_index = 0;
}
public function valid()
{
return isset($this->_content[$this->_index]);
}
public function current()
{
return $this->_content[$this->_index];
}
public function key()
{
return $this->_index;
}
public function next()
{
$this->_index++;
}
}
$array = array('A', 'B', 'C', 'D');
echo "Collection: ";
$aa = new Collection($array);
foreach ($aa as $key => $value) {
if($aa->_index===2){
echo 'loso';
}
else
{
echo "$key => $value. ";
}
}
echo "\n";
?>
/**
* Collection that wraps a numeric array.
* All five public methods are needed to implement
* the Iterator interface.
*/
class Collection implements Iterator
{
private $_content;
public $_index = 0;
public function __construct(array $content)
{
$this->_content = $content;
}
public function rewind()
{
$this->_index = 0;
}
public function valid()
{
return isset($this->_content[$this->_index]);
}
public function current()
{
return $this->_content[$this->_index];
}
public function key()
{
return $this->_index;
}
public function next()
{
$this->_index++;
}
}
$array = array('A', 'B', 'C', 'D');
echo "Collection: ";
$aa = new Collection($array);
foreach ($aa as $key => $value) {
if($aa->_index===2){
echo 'loso';
}
else
{
echo "$key => $value. ";
}
}
echo "\n";
?>