0

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.

fman_29
  • 1
  • 2
  • I don't see anything wrong in that section of code. Do you get a line number in your error message? – droopsnoot Apr 10 '20 at 10:02
  • 1
    There is nothing wrong in the above code, may be problem in your .class.php file code so please post .class.php file as well. – Umar Farooque Khan Apr 10 '20 at 10:09
  • The error will specify exact file and line of code, so please include that. – El_Vanja Apr 10 '20 at 10:26
  • The `$fullPath` is underfined varible. – Dmitry Leiko Apr 10 '20 at 11:24
  • Thanks for responding so quickly! I updated my question hoping to answer some of the questions asked. – fman_29 Apr 10 '20 at 12:14
  • Wait, is this a PHP error on execution or Atom error while editing? – El_Vanja Apr 10 '20 at 12:47
  • El_Vanja, Atom flagged the errors which is why I went back over the code with a fine-tooth comb. But when I run the code on the server, nothing happens. It doesn't throw an error, it just doesn't work. – fman_29 Apr 10 '20 at 12:54
  • Do you have error reporting turned on? Doesn't work meaning what? Blank page? Wrong result? – El_Vanja Apr 10 '20 at 13:00
  • El_Vanja, yes I have error reporting turned on. Blank page is what I'm getting when I run the code. – fman_29 Apr 10 '20 at 13:05
  • Can you give an example of how you're using the Calculator class? The code in the question doesn't perform any actions. – El_Vanja Apr 10 '20 at 13:07
  • El_Vanja, I updated the original question to include every page of code I'm using. – fman_29 Apr 10 '20 at 13:28
  • You might want to check your [error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) settings, because I see errors. As Dmitry pointed out earlier, `$fullPath` is an undefined variable, because you declare it as all lowercase, `$fullpath`. – El_Vanja Apr 10 '20 at 14:17
  • El_Vanja, Thank you! You were a big help. Yes, my error reporting settings were off. I've fixed them. Now, at least, I can figure out the issues! I appreciate your help! – fman_29 Apr 10 '20 at 15:02

0 Answers0