使对象可以像数组一样进行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";
}
上一篇:.NET(C#):XmlArrayItem特性和XmlElement特性在序列化数组的差别


下一篇:ThinkPHP模版验证要注意的地方