Php 构建对象注释树
我想用有限的回复制作评论系统。 例如:Php 构建对象注释树,php,mysql,object,Php,Mysql,Object,我想用有限的回复制作评论系统。 例如: #1st comment ## reply to the 1st comment ## reply to the 1st comment #2nd comment #3rd comment ## reply to the 3rd comment 因此,每个评论都有一个回复树。 最后,我想这样使用它,假设我有来自db的$comments对象数组: foreach($comments as $comment) { echo $comment->
#1st comment
## reply to the 1st comment
## reply to the 1st comment
#2nd comment
#3rd comment
## reply to the 3rd comment
因此,每个评论都有一个回复树。
最后,我想这样使用它,假设我有来自db的$comments对象数组:
foreach($comments as $comment)
{
echo $comment->text;
if($comment->childs)
{
foreach($comment->childs as $child)
{
echo $child->text;
}
}
}
所以我想我需要创建另一个对象,但不知道如何使它全部工作。我应该使用stdClass还是其他什么?
提前感谢。总的来说,我试着把问题放在一边,以理解它,并看看会出现什么类型的OO设计。就我所见,您似乎有三个可识别的对象:要注释的对象、第一级注释和第二级注释
- 要对其进行注释的对象具有一级注释列表
- 第一级注释可以依次包含子注释
- 第二级评论不能包含child
class ObjectThatCanHaveComments
{
protected $comments;
...
public function showAllComments()
{
foreach ($this->comments as $comment)
{
$comment->show();
}
}
}
class FirstLevelComment
{
protected $text;
protected $comments;
...
public function show()
{
echo $this->text;
foreach ($this->comments as $comment)
{
$comment->show();
}
}
}
class SecondLevelComment
{
protected $text;
public function show()
{
echo $this->text;
}
}
这可能是有效的第一种方法。如果这对您的问题有效,您可以通过创建注释模型来改进它,从而删除遍历注释列表的重复代码和$text
定义。注意注释类在show()
消息中已经是多态的