Write object-oriented Program code in PHP to find the factorial of a number.
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 | <?php class Factorial { private $number; // constructor to set the number public function __construct($number) { $this->number = $number; } // calculating the factorial public function calculate() { $t4tutorials = 1; for ($i = 1; $i <= $this->number; $i++) { $t4tutorials *= $i; } return $t4tutorials; } } // create an instance of the Factorial class and pass in the number $t4tutorials = new Factorial(5); // call the calculate method to get the factorial $result = $t4tutorials->calculate(); echo $result; // outputs 120 ?> |