Skip to content

Instantly share code, notes, and snippets.

@eezhal92
Last active February 13, 2016 03:19
Show Gist options
  • Save eezhal92/889ebc29470c965b003e to your computer and use it in GitHub Desktop.
Save eezhal92/889ebc29470c965b003e to your computer and use it in GitHub Desktop.
<?php
class Calculator
{
/**
* Add multiple argument
*
* @return int
*/
public function add()
{
$numbers = func_get_args();
$numbers = array_filter($numbers, function ($num) {
return is_numeric($num);
});
return array_sum($numbers);
}
/**
* Multiply between 2 arg
*
* @return int
*/
public function multiply()
{
$args = $this->validateParam(func_get_args());
list($num1, $num2) = $args;
return $num1 * $num2;
}
/**
* Substract between 2 arg
*
* @return int
*/
public function substract()
{
$args = $this->validateParam(func_get_args());
list($num1, $num2) = $args;
return $num1 - $num2;
}
/**
* Div between 2 arg
*
* @return int
*/
public function division()
{
$args = $this->validateParam(func_get_args());
list($num1, $num2) = $args;
return $num1 / $num2;
}
/**
* Validate method params
*
* @return array
*/
protected function validateParam()
{
$args = func_get_args();
$args = $args[0];
if(count($args) > 2) {
throw new Exception("Just 2 number");
}
return $args;
}
}
// Usage
/*
$cal = new Calculator;
echo 'Add 20 + 30 + 1 = ' . $cal->add(20, 30, 1) . "<br>";
echo 'Multiply 12 * 3 = ' . $cal->multiply(12, 2) . "<br>";
echo 'Substract 12 - 3 = ' . $cal->substract(5, 3) . "<br>";
echo 'Div 100 : 2 = ' . $cal->division(100, 2) . "<br>";
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment