抱歉,如果标题令人困惑,但我正在尝试使用递归函数获取所有评论及其回复。问题是顶级评论对象的数据结构与评论不同。从 访问顶级评论,$comment_object->data->children
而从 访问所有评论回复$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 块来分隔不同的结构?