language: PHP (php 5.4.4)
date: 416 days 12 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
class IteratorTest implements ArrayAccess, Iterator {
  private $pointer = 0;
  private $objects = array();
 
  public function offsetExists($index) {
    return isset($this->objects[$index]);
  }
 
  public function offsetGet($index) {
    return $this->objects[$index];
  }
 
  public function offsetSet($index, $newValue) {
    $this->objects[$index] = $newValue;
  }
 
  public function offsetUnset($index) {
    unset($this->objects[$index]);
  }
 
  public function key() {
    return key($this->objects);
  }
 
  public function current() {
    return current($this->objects);
  }
 
  public function next() {
    next($this->objects);
  }
 
  public function rewind() {
    reset($this->objects);
  }
 
  public function valid() {
    return current($this->objects);
  }
}
 
$it = new IteratorTest();
 
$it['one'] = 1;
$it['two'] = 2;
 
foreach ($it as $k => $v) {
  echo "$k: $v\n";
}
 
// expected result:
// one: 1
// two: 2