我正在尝试OuterIterator
在我的类上实现接口,但我不确定在方法中使用什么getInnerIterator
。我知道它应该返回一个 Iterator 但我不明白在什么情况下它应该返回 anInnerIterator
尽管它已经拥有 Iterator 接口的所有方法,那么为什么它需要一个InnerIterator
?
我完全迷失了OuterIterator
class myouterIterator implements OuterIterator
{
/**
* Returns the inner Iterator for the current entry
*/
public $data = array(1,2,3,4,5,6,7,8,9,10);
public $position = 0;
public function getInnerIterator()
{
// What code should be place here
}
public function rewind()
{
$this->position = 0;
}
public function valid()
{
return isset($this->data[$this->position]);
}
public function key()
{
return $this->position;
}
public function current()
{
return $this->data[$this->position];
}
public function next()
{
++$this->position;
}
}
$OuterIt = new myouterIterator();
foreach($OuterIt as $key => $value)
{
echo $key, ' ', $value, '<br>';
}