-1

I wrote the below code together with a friend. On his computer it worked, on mine it did not. The code seems to be 100% the same. Any ideas why it does not work? I get an error:

Warning: Division by zero in xxxxx/Car.php on line 25.

<?php

class Car{

    private $total_fuel;
    private $curr_fuel;
    private $consumption;




    function fill($quantity){
        $this->curr_fuel += $quantity;
    }
    function go($distance) {
        $needed = $this->consumption/100 * $distance;
        if($this->curr_fuel > $needed){

            $this->curr_fuel -= $needed;
        }else{
            return "Not enough gas for $distance km!". "<br>";
        }
    }
    function fuel_left() {
        return $this->curr_fuel *100 / $this->total_fuel;
    }
}
    function odometer(){
        return $this->km;
    }

    function __construct($total_fuel,$curr_fuel,$consumption) {
        $this->total_fuel = $total_fuel;
        $this->curr_fuel = $curr_fuel;
        $this->consumption = $consumption;

    }



?> 




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>



    <form method="post"> 
        How many km do you want to drive?
        <input type="text" name="distance">
        <input type="submit" value="Go">   
    </form>

    <?php

    session_start();
        if(!isset($_SESSION['car'])){
            $car = new Car(50,25,10);
        }else{
            $car = $_SESSION['car'];
        }

        if(isset($_POST['distance'])){ 
            echo $car->go($_POST['distance']);
            echo "Left fuel: {$car->fuel_left()} % <br>";
            $_SESSION['car'] = $car;

        }

    ?>


</body>
</html>

What could possibly be the issue? Before I create the __construct function everything works fine. I deleted all the cookies, sessions etc, but it still does not work. What could possibly be the mistake?

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
paczek
  • 25
  • 2
  • 2
    Guess on line 25 you are diving a number by zero... Going to hypothesize further and say its this line `return $this->curr_fuel *100 / $this->total_fuel` and that `$this->total_fuel` is zero. – ficuscr Jul 02 '19 at 21:12
  • if your errors were on, you get one regarding sessions, the `session_start()` needs to be moved to before any output –  Jul 02 '19 at 21:35
  • 1
    You have __construct() and odometer() are outside your object. Thus __construct() in never called. – Jason K Jul 02 '19 at 21:42

1 Answers1

2

You've accidentally moved the last two functions outside your class. So, your class gets constructed without its constructor.

} //  <--- your class ends here
    function odometer(){
        return $this->km;
    }

    function __construct($total_fuel,$curr_fuel,$consumption) {
        $this->total_fuel = $total_fuel;
        $this->curr_fuel = $curr_fuel;
        $this->consumption = $consumption;

    }

Apparently, if your friend's code is working and yours isn't, they must not be the same. You should consider sharing a code repository with your friend for this project. With version control this type of difference would be obvious, and it might be a fun thing to learn about as well.

If not for that mistake, it doesn't look like the division by zero error would be possible, unless you constructed a car with zero fuel capacity.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80