All.
I'm following mmtuts YouTube video series on OOP PHP but I keep getting an error message stating that opening and closing parentheses ['(', ')'], and opening and closing brackets ['{', and '}'] are expected even though all are present and appear to be in the correct place.
Can someone please tell me what I'm doing wrong? I've pasted my code below.
The index page is:
<body>
<div ><i class="fas fa-paw" style="color:Navy;size=500%;"></i></div>
<form action="includes/calc.inc.php" method="post">
<p>My Own Calculator</p>
<input type="number" name="num1" placeholder="First number">
<select name="oper">
<option value="add" style="font-size:3em;">+</option>
<option value="sub" style="font-size:3em;">-</option>
<option value="div" style="font-size:3em;">/</option>
<option value="mul" style="font-size:3em;">*</option>
</select>
<input type="number" name="num2" placeholder="Second number">
<button type="submit" class="btn btn-success btn-lg">CALCULATE</button>
</form>
</body>
</html>
This file is called calc.inc.php:
declare(strict_types = 1);
include 'class-autoload.inc.php';
print_r($_POST);
$oper = $_POST["oper"];
$num1 = $_POST["num1"];
$num2 = $_POST["num2"];
$calc = new Calc($oper, (int)$num1, (int)$num2);
try {
echo $calc->calculator();
} catch (TypeError $e) {
echo "ERROR!: " . $e->getMessage();
}
?>
This file is called class-autoload.inc.php"
<?php
spl_autoload_register('myAutoLoader');
function myAutoLoader($className) {
$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if (strpos($url, 'includes') !== false) {
$path = "../classes/";
} else {
$path = "classes/";
}
$extension = ".class.php";
$fullpath = $path . $className . $extension;
if (!file_exists($fullPath)) {
return false;
}
include_once $fullpath;
}
?>
This is the code in the code in the .classes.php file:
<?php
class Calc {
public $operator;
public $num1;
public $num2;
public function __construct(string $oper, int $num1, int $num2) {
$this->operator = $oper;
$this->num1 = $num1;
$this->num2 = $num2;
}
public function calculator() {
switch ($this->operator) {
case 'add':
$result = $this->$num1 + $this->num2;
return $result;
break;
case 'sub':
$result = $this->$num1 - $this->num2;
return $result;
break;
case 'div':
$result = $this->$num1 / $this->num2;
return $result;
break;
case 'add':
$result = $this->$num1 * $this->num2;
return $result;
break;
default:
echo "ERROR!";
break;
}
}
}
?>
The errors are being reported on lines 4 & 6.
I'm using Atom text editor and the only strange thing that I see is on line 5: function myAutoLoader ($className)
appears with yellow parenthesis while all the other parenthetical expressions have white parentheses leading me to think an error has occurred somewhere before this but I don't see anything.