[ create a new paste ] login | about

Link: http://codepad.org/svmPFqf6    [ raw code | output | fork ]

tml - PHP, pasted on Jun 1:
<?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);


Output:
1
2
3
4
5
int(1)
int(2)
int(3)
int(4)
int(5)


Create a new paste based on this one


Comments: