PHP5 Magic Function FizzBuzz


<?PHP

class fizzBuzzInt {

	private $val;

	public function __construct($int) {
		$this->val = $int;
	}

	public function __toString() {
		if ($this->val % 15 == 0) return "FizzBuzz";
		else if ($this->val % 3  == 0) return "Fizz";
		else if ($this->val % 5  == 0) return "Buzz";
		else return "".$this->val."";
	}

	public function intVal() {
		return $this->val;
	}

	public function inc() {
		$this->val++;
	}
}

for ($int = new fizzBuzzInt(1); $int->intVal() <= 100; $int->inc()) {
	echo $int." ";
}

?>

Leave a Reply