0%

php 迭代的一种写法

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
<?php
class Fib implements Iterator{
var $a = 0;
var $b = 1;
var $i = 0;
var $max = 100;

public function __construct($n=100){
$this->max = $n;
}

public function rewind(){
$this->a = 0;
$this->b = 1;
$this->i = 0;
}

public function next(){
$tmp = $this->a + $this->b;
$this->a = $this->b;
$this->b = $tmp;
$this->i = $this->i + 1;
}

public function current(){
return $this->b;
}

public function key(){
return $this->i;
}

public function valid(){
return $this->b < $this->max;
}
}

$a = new Fib(100);
foreach($a as $k=>$v){
echo sprintf("%d\t%d\n", $k, $v);
}

?>

git地址:https://gist.github.com/wjzhangq/838433