使对象可以像数组一样进行foreach循环,要求属性必须是私有
class myIterator implements Iterator
{
private $var = array(1, 2, 3, 4, 5);
public function __construct(){}
public function rewind()
{
reset($this->var);
}
public function current()
{
return current($this->var);
}
public function key()
{
return key($this->var);
}
public function next()
{
return next($this->var);
}
public function valid()
{
return ($this->current() !== false);
}
}
$it = new myIterator;
foreach($it as $key => $value) {
echo "$key=>$value\n";
}
只言片语任我说,提笔句句无需忖。落笔不知寄何人,唯有邀友共斟酌。