Junior — Middle+
Updating the Math class: passing a number to the constructor
livecode
Task condition
It is required to modify the Math class so that when it is instantiated, a single integer value is passed to its constructor. After that, all methods of the class should work with this value without needing to specify the argument each time.
class Math
{
private $value;
public function __construct(int $value)
{
$this->value = $value;
}
public function factorial(): int
{
if ($this->value < 0) {
throw new Exception("value less 0");
}
$resultNum = 1;
for($i = 1; $i <= $this->value; $i++)
{
$resultNum *= $i;
}
return $resultNum;
}
}
$math = new Math(5);
try {
echo $math->factorial();
} catch (Exception $e) {
echo $e->getMessage();
}