forked from weierophinney/PHP-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Iterator.php
49 lines (46 loc) · 1.17 KB
/
Iterator.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
44
45
46
47
48
49
<?php
/**
* Iterator design pattern (Fibonacci numbers)
*
* @author Enrico Zimuel ([email protected])
* @see http://www.php.net/manual/en/class.iterator.php
* @see http://en.wikipedia.org/wiki/Fibonacci_number
*/
class Fibonacci implements Iterator {
protected $value = 0;
protected $sum = 0;
protected $key = 0;
public function rewind() {
$this->value = 0;
$this->key = 0;
}
public function current() {
return $this->value;
}
public function key() {
return $this->key;
}
public function next() {
if ($this->value === 0) {
$this->value = 1;
} else {
$old = $this->value;
$this->value += $this->sum;
$this->sum = $old;
}
$this->key++;
}
public function valid() {
return ($this->value < PHP_INT_MAX);
}
}
// print the Fibonacci numbers until PHP_INT_MAX
foreach ($test = new Fibonacci() as $key => $value) {
printf("%d) %d\n", $key, $value);
}
// print the first 10 Fibonacci's numbers
$num = new Fibonacci();
for ($i = 0; $i < 10; $i++) {
printf("%d) %d\n", $i, $num->current());
$num->next();
}