PHP:无法重新声明类 Comment::GetComment [重复]
Posted
技术标签:
【中文标题】PHP:无法重新声明类 Comment::GetComment [重复]【英文标题】:PHP: Cannot redeclare class Comment::GetComment [duplicate] 【发布时间】:2020-05-08 23:58:28 【问题描述】:有人可以纠正我的代码吗? 错误是“致命错误:无法重新声明 Comment::getComment()” 如果错误在这个文件中,你能告诉我或更正吗?否则告诉我如何解决这个问题 谢谢你。
<?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();
编辑:这是另一个文件,我怎样才能消除这个错误?我想保留这两个功能
<?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.'"');
?>
【问题讨论】:
请显示您的评论课程。最有可能的是,您已经在那里声明了两次 getComment。public function getComment()
和 public static function getComment(int $id_post)
(getComments 也是如此)。 PHP 不支持这种类型的函数重载。类中的每个函数都必须具有唯一的名称。
【参考方案1】:
PHP 中不允许重载。您不能有两个具有相同名称和不同参数的函数。请看一下这个页面: PHP two methods with the same name
【讨论】:
以上是关于PHP:无法重新声明类 Comment::GetComment [重复]的主要内容,如果未能解决你的问题,请参考以下文章
PHP:无法重新声明类 Comment::GetComment [重复]
Laravel 8 Custom Helper function PHP致命错误:无法重新声明以前在C:(path)Helpers.php中声明的functionName() [重复]