Skip to content

Instantly share code, notes, and snippets.

@wjzhangq
Created February 22, 2011 09:51
Show Gist options
  • Save wjzhangq/838433 to your computer and use it in GitHub Desktop.
Save wjzhangq/838433 to your computer and use it in GitHub Desktop.
<?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);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment