【发布时间】:2012-09-09 20:30:31
【问题描述】:
对不起,如果标题令人困惑,但我正在尝试使用递归函数获取所有 cmets 及其回复。问题是顶级评论对象具有与 cmets 不同的数据结构。从$comment_object->data->children 访问顶级cmets,而从$comment->data->replies 访问所有评论回复。这是我目前所拥有的:
public function get_top_comments()
{
$comments_object = json_decode(file_get_contents("http://www.reddit.com/comments/$this->id.json"));
sleep(2); // after every page request
$top_comments = array();
foreach ($comments_object[1]->data->children as $comment)
{
$c = new Comment($comment->data);
$top_comments[] = $c;
}
return $top_comments;
}
public function get_comments($comments = $this->get_top_comments) //<-- doesn't work
{
//var_dump($comments);
foreach ($comments as $comment)
{
if ($comment->data->replies != '')
{
//Recursive call
}
}
}
我尝试将$comments = $this->get_top_comments 指定为递归函数的默认参数,但我猜PHP 不支持这个?我是否必须在函数中使用 if-else 块来分隔不同的结构?
【问题讨论】:
标签: php recursion overloading reddit