I am building an OOP calculator in PHP and I think I started off decent. I have an HTML-file:
<?php
require_once("includes/initialize.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>Huiswerk week 2</title>
<meta charset="utf-8"/>
</head>
<body>
<?php if (isset($error)) : ?>
<span class="error"><?php print $error; ?></span>
<?php endif; ?>
<?php if (empty($calculation)) : ?>
<form action="index.php" method="POST">
<fieldset>
<legend>Rekenmachine</legend>
<label for="first-number">Getal 1</label><input type="number" id="first-number" name="first-number "/>
<label for="second-number">Getal 2</label><input type="number" id="second-number" name="second-number"/>
<input type="radio" id="countUp" value="countUp" name="option"/><label for="countUp">+</label>
<input type="radio" id="countDown" value="countDown" name="option"/><label for="countDown">-</label>
<input type="radio" id="divide" value="divide" name="option"/><label for="divide">/</label>
<input type="radio" id="multiply" value="multiply" name="option"/><label for="multiply">*</label>
<input type="submit" id="submit" value="Uitvoeren"/>
</fieldset>
</form>
<?php endif; ?>
<?php
if (is_numeric($_POST["first-number"]) && is_numeric($_POST["second-number"])) {
switch ($_POST["option"]) {
case "countUp":
$calc = new Calculator();
$result = $calc->countUp($_POST["first-number"], $_POST["second-number"]);
echo $result;
break;
case "countDown":
$calc = new Calculator();
$result = $calc->countDown($_POST["first-number"], $_POST["second-number"]);
echo $result;
break;
case "divide":
$calc = new Calculator();
$result = $calc->divide($_POST["first-number"], $_POST["second-number"]);
echo $result;
break;
case "multiply":
$calc = new Calculator();
$result = $calc->divide($_POST["first-number"], $_POST["second-number"]);
echo $result;
break;
default:
$error = "Something went terribly wrong.";
echo $error;
break;
}
} else {
$error = "One of the given numbers is not numeric.";
echo $error;
}
?>
</body>
</html>
The include from the index.php is as follows:
<?php
// Require needed files
require_once "settings.php";
require_once "classes/Calculator.php";
try{
} catch (Exception $e){
$error = "Oops, try to fix your error please: " . $e->getMessage() . " on line " . $e->getLine() . " of " . $e->getFile();
}
And the Calculator class is defined in this file:
<?php
/**
* Class Calculator
*/
class Calculator {
public function __construct(){
}
/**
* @param $firstNumber
* @param $secondNumber
* @return mixed
*/
public function countUp($firstNumber, $secondNumber){
$result = $firstNumber + $secondNumber;
return $result;
}
/**
* @param $firstNumber
* @param $secondNumber
* @return mixed
*/
public function countDown($firstNumber, $secondNumber){
$result = $firstNumber - $secondNumber;
return $result;
}
/**
* @param $firstNumber
* @param $secondNumber
* @return float
*/
public function divide($firstNumber, $secondNumber){
$result = $firstNumber / $secondNumber;
return $result;
}
/**
* @param $firstNumber
* @param $secondNumber
* @return mixed
*/
public function multiply($firstNumber, $secondNumber){
$result = $firstNumber * $secondNumber;
return $result;
}
}
The assignment is to have a form that collects two values and four options (+, -, /, *). The given numbers will be used for the chosen calculation. The calculator has to be OOP, and when an error occurs (like when input is not a number), the page will go back to the form with the two inputs intact.
How should I start off from here? I have tried declaring two vars and using a switch in the Calculator class, but it would not work.
Your form as it stands will do nothing. An input like this:
<input type="number" id="first-number" />
must have a name
attribute in order to return a value. Try this:
<input type="number" id="first-number" name="first-number" />
The id
is still useful for attaching per-item events and attaching label
s, so I would keep that in.
Secondly, you are accessing the $_POST
superglobal even when viewing the page normally i.e. in a get
operation. One solution to that is to wrap your existing if
statement thusly:
if ($_POST) {
if (is_numeric($_POST["first-number"]) && is_numeric($_POST["second-number"])) {
// ...
}
}
That way, you'll only be accessing post
data when the user submits the form.