A
A
Akmal Islamov2018-10-17 16:51:48
PHP
Akmal Islamov, 2018-10-17 16:51:48

What is wrong in this code?

I started to study OOP in php, I write the code as they say in the video lesson, but an error occurs:

Fatal error: Class Car contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Car::showInfo) in C:\OSPanel\domains\localhost\index.php on line 30

Here is the code:
<?php 
    class Car {
        public $name;
        public $year;
        public $type;
        protected $hp;
        protected $price;

        

        function con ($name,$year, $type, $hp, $price) {
            $this->name = $name;
            $this->year = $year;
            $this->type = $type;
            $this->hp = $hp;
            $this->price = $price;

        }

        public function tuning() {
            $this->hp += 50;
        }
        public function showHp() {
            echo $this->hp;
        }

        abstract public function showInfo();

        
    }

    class Mustang extends Car {
        public function tuning() {
            parent::tuning();
            $this->hp += 450;
        }
        public function showInfo() {
            echo $this->name;
        }
    }

    $mustangGT = new Mustang();

    $mustangGT->con('Mustang GT', 2017, 'Muscle car', '900' , 39000);
    echo $mustangGT->showHp();
    $mustangGT->tuning();
    echo "<br>";
    echo $mustangGT->showHp();    
    ?>

What did I misunderstand?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Bille, 2018-10-17
@Akmal_23ram

The Car class has an abstract method, the class must also be declared abstract

T
toxa82, 2018-10-17
@toxa82

The Car class must be abstract because contains an abstract method.

Y
yegreS, 2018-10-17
@yegreS

and it doesn't hurt to use __construct instead of con

X
xmoonlight, 2018-10-17
@xmoonlight

https://phpenthusiast.com/object-oriented-php-tutorials-07 .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question