<?php
class Fibonnaci implements Iterator
{
private $last;
private $beforeLast;
private $index = 0;
public function rewind()
{
$this->beforeLast = 0;
$this->last = 1;
$this->index = 0;
}
public function current()
{
return $this->beforeLast + $this->last;
}
public function key()
{
return $this->index;
}
public function valid()
{
return $this->current() < 100;
}
public function next()
{
$last = $this->last;
$this->last = $this->current();
$this->beforeLast = $last;
}
}
$fib = new Fibonnaci();
foreach($fib as $nextNumber)
echo $nextNumber, ' ';
// foreach equals to the next WHILE loop
$fib->rewind();
while($fib->valid())
{
$key = $fib->key();
$val = $fib->current();
/// echo
$fib->next();
}
1