<?php # Base Iterator+ArrayAccess implementation
class myIt implements Iterator, ArrayAccess {
private $position=0;
protected $data = array();
public function current() {
return $this->data[$this->position];
}
public function key() {
return $this->position;
}
public function next() {
++$this->position;
}
public function valid() {
return isset($this->data[$this->position]);
}
public function rewind() {
$this->position = 0;
}
public function offsetExists($offset) { return isset($this->data[$offset]); }
public function offsetGet($offset) { return $this->data[$offset]; }
public function offsetUnset($offset) { unset($this->data[$offset]); }
public function offsetSet($offset, $entity) {
if (is_null($offset)) $this->data[] = $entity;
else $this->data[$offset] = $entity;
}
}
$it = new myIt();
foreach(range(1,5) as $n) $it[] = $n;
foreach($it as $iter) var_dump($iter);