I aim to improve my PHP OOP skills and made a simple calculator. Visitors can choose 2 numbers and a prefix, so they can add, subtract, divide and so forth. The output is the sum. The good thing is it's working, but is it also clean coding? Also, is it wise to use the switch
statement in a function?
I hope to get some tips so I can improve and learn.
class calcdata {
public $number1;
public $prefix;
public $number2;
public function __construct() {
$this->number1 = $number1 = $_POST['number1'];
$this->prefix = $prefix = $_POST['prefix'];
$this->number2 = $number2 = $_POST['number2'];
}
public function result(){
switch($this->prefix){
case "+":
return ($this->number1 + $this->number2);
break;
case "-":
return ($this->number1 - $this->number2);
break;
case "/":
return ($this->number1 / $this->number2);
break;
case "*":
return ($this->number1 * $this->number2);
break;
default:
return "Only use: \" +, -, / of * \" ";
}
}
}
$show = new calcdata();
echo $show->result();
</body>
</html>