iterator impliment php

Solutions on MaxInterview for iterator impliment php by the best coders in the world

showing results for - "iterator impliment php"
Chrystal
31 Feb 2016
1
2<?php
3class myIterator implements Iterator {
4    private $position 0;
5    private $array array(
6        "firstelement",
7        "secondelement",
8        "lastelement",
9    );  
10
11    public function __construct({
12        $this->position = 0;
13    }
14
15    public function rewind({
16        var_dump(__METHOD__);
17        $this->position = 0;
18    }
19
20    public function current({
21        var_dump(__METHOD__);
22        return $this->array[$this->position];
23    }
24
25    public function key({
26        var_dump(__METHOD__);
27        return $this->position;
28    }
29
30    public function next({
31        var_dump(__METHOD__);
32        ++$this->position;
33    }
34
35    public function valid({
36        var_dump(__METHOD__);
37        return isset($this->array[$this->position]);
38    }
39}
40
41$it new myIterator;
42
43foreach($it as $key => $value) {
44    var_dump($key$value);
45    echo "\n";
46}
47?>
48
49  
50  
51  // output
52  
53string(18) "myIterator::rewind"
54string(17) "myIterator::valid"
55string(19) "myIterator::current"
56string(15) "myIterator::key"
57int(0)
58string(12) "firstelement"
59
60string(16) "myIterator::next"
61string(17) "myIterator::valid"
62string(19) "myIterator::current"
63string(15) "myIterator::key"
64int(1)
65string(13) "secondelement"
66
67string(16) "myIterator::next"
68string(17) "myIterator::valid"
69string(19) "myIterator::current"
70string(15) "myIterator::key"
71int(2)
72string(11) "lastelement"
73
74string(16) "myIterator::next"
75string(17) "myIterator::valid"
76
77