public function execute($operatorName) { if ($operatorName == "+") { $addobject = new Add(); $addobject->execute($this->values); //$this->add(); } else { if ($operatorName == "-") { $subtractobject = new Subtract(); $subtractobject->execute($this->values); //$this->subtract (); } else { if ($operatorName == "!") { $factorialobject = new Factorial(); $factorialobject->execute($this->values); //$this->factorial (); } else { try { throw new NoSuchOperator('No such operator!'); } catch (NoSuchOperator $e) { echo 'Caught exception: ' . $e->getMessage(); } } } } }
<?php class Factorial { private $input; public function __construct($input) { $this->input = $input; } public function execute() { $answer = 1; $x = 1; while ($x <= $this->input) { $answer = $answer * $x; $x = $x + 1; } return $answer; } } $f = new Factorial(10); $result = $f->execute();