Class incompatible with the interface

Asked

Viewed 33 times

0

I am trying to modify some methods of the class in the subclass but I get the following error:

Fatal error:  Declaration of Filme::create($name, $year, $poster, $description, $genre, $trailer, $download) must be compatible with InterfaceMedia::create($name, $year, $poster, $description, $genre, $trailer) in /var/www/html/cinetor/class/php/ClassFilme.php on line 20

PARENT CLASSES:

    <?php

interface InterfaceMedia {

    public function load($id);

    public function drop($id);

    public function create($name, $year, $poster, $description, $genre, $trailer);

    public function update();

    public function getAll();

    public function setName($name);

    function setYear($year);

    function setPoster($poster);

    function setDescription($description);

    function setGenre($genre);

    function setTrailer($trailer);

    function getName();

    function getYear();

    function getPoster();

    function getDescription();

    function getGenre();

    function getTrailer();

    public function getViews();
}

abstract class Media implements InterfaceMedia {

    protected $id;
    private $name;
    private $year;
    private $poster;
    private $description;
    private $genre;
    private $trailer;
    private $views;
    private $db;
    private $log;

    function __construct() {
        require_once $_SERVER['DOCUMENT_ROOT'] . '/cinetor/data/data.php';
        $this->db = connectdb();
        $this->views = 0;
    }

    private function loadMedia($c) {
        $this->id = $c["id"];
        $this->name = $c["name"];
        $this->year = $c["year"];
        $this->poster = $c["poster"];
        $this->description = $c["description"];
        $this->genre = $c["genre"];
        $this->trailer = $c["trailer"];
        $this->views = $c["views"];
    }

    public function load($id) {
        $srh = $this->db->prepare("SELECT * FROM `` WHERE id = :id ");
        $srh->bindValue("id", $id, PDO::PARAM_INT);
        $srh->execute();
        if ($srh->rowCount > 0) {
            $this->loadMedia($srh->fetch(PDO::FETCH_ASSOC));
            $return = TRUE;
        } else {
            $return = FALSE;
        }
        return $return;
    }

    public function drop($id) {
        $drop = $this->db->prepare("DELTE FROM `` WHERE id = :id");
        $drop->bindValue("id", $id, PDO::PARAM_INT);
        $drop->execute();
        if ($drop->rowCount() > 0) {
            $this->log = "midia has been deleted";
            $return = TRUE;
        } else {
            $this->log = "Cannot possible delete media " . $id;
            $return = FALSE;
        }
        return $return;
    }

    private function checkMedia($name) {
        $chk = $this->db->prepare("SELECT `nome` FROM `` WHERE name = :name ");
        $chk->bindValue("id", $name, PDO::PARAM_STR);
        $chk->execute();
        if ($chk->rowCount() > 0) {
            $return = TRUE;
        } else {
            $return = FALSE;
        }
        return $return;
    }

    private function createMedia() {
        if ($this->checkMedia($this->name)) {
            $crate = $this->db->prepare("INSERT INTO `` (`name`,`year`,`poster`,`description`,`genre`,`trailer`) VALUES ( :name , :year , :poster , :description, :genre , :trailer) ");
            $crate->bindValue("name", $this->name, PDO::PARAM_STR);
            $crate->bindValue("year", $this->year, PDO::PARAM_STR);
            $crate->bindValue("poster", $this->poster, PDO::PARAM_STR);
            $crate->bindValue("description", $this->description, PDO::PARAM_STR);
            $crate->bindValue("genre", $this->genre, PDO::PARAM_STR);
            $crate->bindValue("trailer", $this->trailer, PDO::PARAM_STR);
            $crate->execute();
            if ($this->db->lastInsertId() > 0) {
                $this->id = $this->db->lastInsertId();
                $this->log = "Media has been created.";
                $return = TRUE;
            } else {
                $this->log = "SQL ERROR. " . $crate->errorInfo();
                $return = FALSE;
            }
        } else {
            $this->log = "Media already exist!";
            $return = FALSE;
        }
        return $return;
    }

    public function create($name, $year, $poster, $description, $genre, $trailer) {
        $this->name = $name;
        $this->year = $year;
        $this->poster = $poster;
        $this->description = $description;
        $this->genre = $genre;
        $this->trailer = $genre;
        $this->trailer = $trailer;
        return $this->createMedia();
    }

    public function update() {
        $up = $this->db->prepare("UPDATE `` SET `name` = :name ,`year` = :year ,`poster` = :poster ,`description` = :description ,`genre` = :genre ,`trailer` = :trailer , `views` = :viwes WHERE id = :id");
        $up->bindValue("name", $this->name, PDO::PARAM_STR);
        $up->bindValue("year", $this->year, PDO::PARAM_STR);
        $up->bindValue("poster", $this->poster, PDO::PARAM_STR);
        $up->bindValue("description", $this->description, PDO::PARAM_STR);
        $up->bindValue("genre", $this->genre, PDO::PARAM_STR);
        $up->bindValue("trailer", $this->trailer, PDO::PARAM_STR);
        $up->bindValue("views", $this->views, PDO::PARAM_INT);
        $up->bindValue("id", $this->id, PDO::PARAM_INT);
        $up->execute();
        if ($up->rowCount() > 0) {
            $this->log = "Updated with success.";
            $return = TRUE;
        } else {
            $this->log = "Cannot update!";
            $return = FALSE;
        }
        return $return;
    }

    function setName($name) {
        $this->name = $name;
    }

    function setYear($year) {
        $this->year = $year;
    }

    function setPoster($poster) {
        $this->poster = $poster;
    }

    function setDescription($description) {
        $this->description = $description;
    }

    function setGenre($genre) {
        $this->genre = $genre;
    }

    function setTrailer($trailer) {
        $this->trailer = $trailer;
    }

    function getName() {
        return $this->name;
    }

    function getYear() {
        return $this->year;
    }

    function getPoster() {
        return $this->poster;
    }

    function getDescription() {
        return $this->description;
    }

    function getGenre() {
        return $this->genre;
    }

    function getTrailer() {
        return $this->trailer;
    }

    function getViews() {
        return $this->views;
    }


    public function getAll() {
        $array = [
            'name' => $this->name,
            'type' => $this->type,
            'year' => $this->year,
            'poster' => $this->poster,
            'description' => $this->description,
            'genre' => $this->genre,
            'trailer' => $this->trailer,
            'views' => $this->views
        ];
        return $array;
    }

}

Child class:

    <?php

require_once 'ClassMedia.php';


class Filme extends Media {

    private $download;

    private function loadMedia($c) {
        $this->id = $c["id"];
        $this->name = $c["name"];
        $this->year = $c["year"];
        $this->poster = $c["poster"];
        $this->description = $c["description"];
        $this->genre = $c["genre"];
        $this->trailer = $c["trailer"];
        $this->views = $c["views"];
        $this->download = $c["download"];
    }

    private function createMedia() {
        if ($this->checkMedia($this->name)) {
            $crate = $this->db->prepare("INSERT INTO `` (`name`,`year`,`poster`,`description`,`genre`,`trailer`,`download`) VALUES ( :name , :year , :poster , :description, :genre , :trailer, :download) ");
            $crate->bindValue("name", $this->name, PDO::PARAM_STR);
            $crate->bindValue("year", $this->year, PDO::PARAM_STR);
            $crate->bindValue("poster", $this->poster, PDO::PARAM_STR);
            $crate->bindValue("description", $this->description, PDO::PARAM_STR);
            $crate->bindValue("genre", $this->genre, PDO::PARAM_STR);
            $crate->bindValue("trailer", $this->trailer, PDO::PARAM_STR);
            $crate->bindValue("download", $this->download, PDO::PARAM_STR);
            $crate->execute();
            if ($this->db->lastInsertId() > 0) {
                $this->id = $this->db->lastInsertId();
                $this->log = "Media has been created.";
                $return = TRUE;
            } else {
                $this->log = "SQL ERROR. " . $crate->errorInfo();
                $return = FALSE;
            }
        } else {
            $this->log = "Media already exist!";
            $return = FALSE;
        }
        return $return;
    }

    public function create($name, $year, $poster, $description, $genre, $trailer, $download) {
        $this->name = $name;
        $this->year = $year;
        $this->poster = $poster;
        $this->description = $description;
        $this->genre = $genre;
        $this->trailer = $genre;
        $this->trailer = $trailer;
        $this->download = $download;
        return $this->createMedia();
    }

    public function update() {
        $up = $this->db->prepare("UPDATE `` SET `name` = :name ,`year` = :year ,`poster` = :poster ,`description` = :description ,`genre` = :genre ,`trailer` = :trailer , `views` = :viwes , `download` = :download WHERE id = :id");
        $up->bindValue("name", $this->name, PDO::PARAM_STR);
        $up->bindValue("year", $this->year, PDO::PARAM_STR);
        $up->bindValue("poster", $this->poster, PDO::PARAM_STR);
        $up->bindValue("description", $this->description, PDO::PARAM_STR);
        $up->bindValue("genre", $this->genre, PDO::PARAM_STR);
        $up->bindValue("trailer", $this->trailer, PDO::PARAM_STR);
        $up->bindValue("views", $this->views, PDO::PARAM_INT);
        $up->bindValue("id", $this->id, PDO::PARAM_INT);
        $up->bindValue("download", $this->download, PDO::PARAM_STR);
        $up->execute();
        if ($up->rowCount() > 0) {
            $this->log = "Updated with success.";
            $return = TRUE;
        } else {
            $this->log = "Cannot update!";
            $return = FALSE;
        }
        return $return;
    }

    function getDownload() {
        return $this->download;
    }

    function setDownload($download) {
        $this->download = $download;
    }

    public function getAll() {
        $array = [
            'name' => $this->name,
            'type' => $this->type,
            'year' => $this->year,
            'poster' => $this->poster,
            'description' => $this->description,
            'genre' => $this->genre,
            'trailer' => $this->trailer,
            'views' => $this->views,
            'download'=> $this->download
        ];
        return $array;
    }

}
  • 4

    You entered a parameter $download which is not defined in the interface. You should remove it or add it to the interface if you have forgotten. The parameters should be exactly the same.

1 answer

2


Interface is like a "contract". The interface defines what the MUST class has.

In the method create() class Filme{}, there is an additional parameter ($download) relative to the same method defined in the interface.

Remove this parameter or add it to the interface.

example:

interfacemedia interface {

public function load($id);

public function drop($id);

public function create($name, $year, $poster, $description, $genre, $trailer, $download);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.