PHP迭代器Iterator介面

餘二五發表於2017-11-22

介紹

可在內部迭代自己的外部迭代器或類的介面。

1
2
3
4
5
6
7
8
9
10
   
Iterator extends Traversable
{
    /* 方法 */
    abstract public mixed current(void)
    abstract public scalar key(void)
    abstract public void next(void)
    abstract public void rewind(void )
    abstract public boolean valid(void)
}


舉個例子,定義一個迭代器:

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
55
56
57
58
59
class myIerator implements Iterator
{
    protected $position;//指標
    public $arr = [];
 
    public function __construct(array $arr)
    {
        $info date(`Y-m-d H:i:s`) . ` ` __METHOD__ ` Line :` __LINE__ "
"
;
        $this->writeLog($info);
        $this->arr = $arr;
        return $this;
    }
 
    //獲取當前的值
    public function current()
    {
        $info date(`Y-m-d H:i:s`) . ` ` __METHOD__ ` Line :` __LINE__ "
"
;
        $this->writeLog($info);
        return $this->arr[$this->position] ?? null;
    }
 
    //將指標移至下一位
    public function next()
    {
        $info date(`Y-m-d H:i:s`) . ` ` __METHOD__ ` Line :` __LINE__ "
"
;
        $this->writeLog($info);
        return ++$this->position;
    }
 
    //返回當前的指標
    public function key()
    {
        $info date(`Y-m-d H:i:s`) . ` ` __METHOD__ ` Line :` __LINE__ "
"
;
        $this->writeLog($info);
        return $this->position;
    }
 
 
    //重置指標
    public function rewind()
    {
        $info date(`Y-m-d H:i:s`) . ` ` __METHOD__ ` Line :` __LINE__ "
"
;
        $this->writeLog($info);
        $this->position = 0;
    }
 
    //檢查當前是否有效
    public function valid()
    {
        $info date(`Y-m-d H:i:s`) . ` ` __METHOD__ ` Line :` __LINE__ "
"
;
        $this->writeLog($info);
        return isset($this->arr[$this->position]);
    }
 
    private function writeLog(string $info)
    {
        error_log($info, 3, `./debug.log`);
    }
}


1
2
3
4
5
$zhangsan=[`one`,`two`,`three`,`four`];
$arr new myIerator($zhangsan);
foreach ($arr as $k => $v) {
    echo "{$k} => {$v} 
"
;
}

列印結果:

0 => one 

1 => two 

2 => three 

3 => four 


檢視呼叫日誌:

2017-11-27 15:56:04 myIerator::__construct Line :16


2017-11-27 15:56:04 myIerator::rewind Line :50    重置指標

2017-11-27 15:56:04 myIerator::valid Line :58        校驗是否有效

2017-11-27 15:56:04 myIerator::current Line :25    獲取當前指標的值

2017-11-27 15:56:04 myIerator::key Line :41          獲取當前指標位置

2017-11-27 15:56:04 myIerator::next Line :33        指標下移一位(進入下一個迴圈)


2017-11-27 15:56:04 myIerator::valid Line :58

2017-11-27 15:56:04 myIerator::current Line :25

2017-11-27 15:56:04 myIerator::key Line :41

2017-11-27 15:56:04 myIerator::next Line :33


2017-11-27 15:56:04 myIerator::valid Line :58

2017-11-27 15:56:04 myIerator::current Line :25

2017-11-27 15:56:04 myIerator::key Line :41

2017-11-27 15:56:04 myIerator::next Line :33


2017-11-27 15:56:04 myIerator::valid Line :58

2017-11-27 15:56:04 myIerator::current Line :25

2017-11-27 15:56:04 myIerator::key Line :41

2017-11-27 15:56:04 myIerator::next Line :33


2017-11-27 15:56:04 myIerator::valid Line :58




//遍歷方式二:
$arr->rewind();
while ($arr->valid()) {
   $key = $arr->key();
   $value = $arr->current();

   echo "{$key} => {$value} ";

   $arr->next();
}

本文轉自 hgditren 51CTO部落格,原文連結:http://blog.51cto.com/phpme/2044921,如需轉載請自行聯絡原作者


相關文章