0

Can someone correct me my code? The error is " Fatal error: Cannot redeclare Comment::getComment() " if the error is in this file, can you show it to me or correct it? Otherwise tell me how I can fix this Thanks you.

<?php  
require_once(__DIR__.'/../model/article_model.php');
require_once(__DIR__.'/../model/comments_model.php');
require_once(__DIR__.'/../model/espaceadmin_model.php');

class CommentsController {

    public static function commentsPage () {
            if(isset($_POST['deletecomments'])) {
            $deletecomments= Comment::getComment((int) $_POST['post_id']);
            $deletecomments->deletecomment();
    }
        $template = file_get_contents(__DIR__.'/../view/backend/template-admin.html');
        $view = file_get_contents(__DIR__.'/../view/backend/commentsadmin_view.html');
        $loop_content = file_get_contents(__DIR__.'/../view/backend/commentsadmin_articles.html');
        $articles = Article::getArticles();


        $articleListe = "";
        foreach ($articles as $article) {
            $articleListe.=  str_replace('{ARTICLE_TITLE}', $article->getTitre(), $loop_content);
            $articleListe =  str_replace('{ARTICLE_DATE}', $article->getDate(), $articleListe);
            $articleListe =  str_replace('{ARTICLE_CONTENT}', $article->getContenu(), $articleListe);

            $comments = Comment::getComments($article->getId());
        foreach ($comments as $comment) {
            $articleListe =  str_replace('{COMMENTS_AUTHOR}', $comment->getAuthor(), $articleListe);
            $articleListe =  str_replace('{COMMENTS_DATE}', $comment->getComment_Date(), $articleListe);
            $articleListe =  str_replace('{COMMENTS_COMMENT}',$comment->getComment(), $articleListe);
            $articleListe = str_replace('{COMMENT_POSTID}', $comment->getPost_id(), $articleListe);
        }
        }

        $view = str_replace('{BOUCLE}', $articleListe, $view);
        $template = str_replace('{CONTENT}', $view, $template);
        $template = str_replace('{TITLE}', "Index", $template);

        print($template);
    }

}

CommentsController::commentsPage();

edit: here is the other file, how can I remove this error? I would like to keep both functions

<?php 

class Comment { 

    private $id = null;
    private $post_id;
    private $author;
    private $comment;
    private $comment_date;

    public function getId(){
        return $this->id;
    }

    public function getPost_id(){
        return $this->post_id;
    }

    public function setPost_id($post_id){
        $this->post_id = $post_id;
    }

    public function getAuthor(){
        return $this->author;
    }

    public function setAuthor($author){
        $this->author = $author;
    }

    public function getComment(){
        return $this->comment;
    }

    public function setComment($comment){
        $this->comment = $comment;
    }

    public function getComment_date(){
        return $this->comment_date;
    }

    public function setComment_date($comment_date){
        $this->comment_date = $comment_date;

    }

    public function persist(){
        if($this->id==null) {
            $this->insert();
        } else {
            $this->update();
        }
    }

    private function insert(){
        $bdd = Database::getDb();
        $query = "INSERT INTO comments(post_id, author, comment, comment_date) VALUES(?,?,?,?)";
        $stmt = $bdd->prepare($query); 
        $stmt->execute([
            $this->post_id, 
            $this->author,
            $this->comment,
            $this->comment_date
        ]);
        $this->id=$bdd->lastInsertId();
        return true;
    }

    /* private function update() { 
        $bdd = Database::getDb();
        $query = "UPDATE comments SET comment = :comment WHERE id = :id";
    UPDATE ... WHERE id = ?
    } */

    public static function getComments($id_post) {

        $bdd = Database::getDb(); 
        $reponse = $bdd->query('SELECT * FROM comments WHERE post_id = "'.$id_post.'"');

        $result= array();
        while ($donnees = $reponse->fetch())
        {
            $currentArticle= new Comment();
            $currentArticle->id = $donnees['id'];
            $currentArticle->setPost_id($donnees['post_id']);
            $currentArticle->setAuthor($donnees['author']);
            $currentArticle->setComment($donnees['comment']);
            $currentArticle->setComment_date($donnees['comment_date']);

            array_push($result, $currentArticle);
        }
        return $result; 
    }


    public static function getComment(int $id_post) {
        $bdd = Database::getDb(); 
        $reponse = $bdd->query('SELECT * FROM comments WHERE post_id = "'.$id_post.'"');

        while ($donnees = $reponse->fetch())
        {
            $currentArticle= new Comment();
            $currentArticle->id = $donnees['id'];
            $currentArticle->setPost_id($donnees['post_id']);
            $currentArticle->setAuthor($donnees['author']);
            $currentArticle->setComment($donnees['comment']);
            $currentArticle->setComment_date($donnees['comment_date']);
            return $currentArticle;
        }
        return null; 
    }

public function deletecomment() {
            $bdd = Database::getDb();
            $bdd->query('DELETE FROM comments WHERE post_id = "'.(int)$id_post.'"');
    }
}
?>
Myllow
  • 1
  • 2
  • 1
    Show your Comment class, please. Most likely, you have getComment declared twice there. – aynber Jan 22 '20 at 18:39
  • 1
    `public function getComment(){` and `public static function getComment(int $id_post) {` (same goes for getComments). PHP does not support this type of function overloading. Each function in the class must be uniquely named. – aynber Jan 22 '20 at 18:49

1 Answers1

0

overloading is not allowed in PHP. You can not have two functions with the same name and different arguments. Please have a look at this page: PHP two methods with the same name