<?php
class Traverable {
}
abstract class Iterator extends Traverable {
abstract public function current();
abstract public function key();
abstract public function next();
abstract public function rewind();
abstract public function valid();
}
class myIterator implements Iterator
{
private $position = 0;
private $array = [
"firstelement",
"secondelement",
"lastelement",
];
public function __construct()
{
$this->position = 0;
}
function rewind()
{
var_dump(__METHOD__);
$this->position = 0;
}
function current()
{
var_dump(__METHOD__);
return $this->array[$this->position];
}
function key()
{
var_dump(__METHOD__);
return $this->position;
}
function next()
{
var_dump(__METHOD__);
++$this->position;
}
function valid()
{
var_dump(__METHOD__);
return isset($this->array[$this->position]);
}
}
$it = new myIterator;
foreach ($it as $key => $value) {
var_dump($key, $value);
echo "\n";
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結