Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8497463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:07:58+00:00 2026-06-11T00:07:58+00:00

Trying to grok object-oriented PHP. I have a recursive method to return comments and

  • 0

Trying to grok object-oriented PHP. I have a recursive method to return comments and their replies and compile them to a flat array $comments_list.

<?php

class RedditPosts
{
    public function get_post_ids($from, $limit)
    {
        // GET POSTS
        $list = json_decode(file_get_contents("http://www.reddit.com/$from.json?limit=$limit"));
        sleep(2); //after every page request

        $post_ids = array();
        foreach($list->data->children as $post) {
            $post_ids[] = $post->data->id;
        }
        return $post_ids;
    }
}

class RedditComments
{
    static $comments_list = array();

    public function get_comments($post_id)
    {
        $comments_object = json_decode(file_get_contents("http://www.reddit.com/comments/$post_id.json"));
        sleep(2);

        $top_comments = $comments_object[1]->data->children;
        //var_dump($top_comments);
        self::get_sub_comments($top_comments);
    }

    static function get_sub_comments($root_comments)
    {
        foreach($root_comments as $comment)
        {
            self::$comments_list[] = $comment;
            //echo $comment->data->body . "<br/>"

            if ($comment->data->replies != '')
            {
                self::get_sub_comments($comment->data->replies->data->children);
            }
        }
        var_dump(self::$comments_list);
        return self::$comments_list;

    }
}

/******************************MAIN************************************/

$ps = new RedditPosts();
$my_posts = $ps->get_post_ids("r/learnprogramming", 2);

$cm = new RedditComments();
$my_comments = $cm->get_comments($my_posts[0]);
var_dump($my_comments);

?>

I do a var_dump right before returning it and it is filled and looks correct but when I call it outside the method it is null. Probably an issue with scope but I’m new at this and cant figure out where and I’ve hit a wall. Help appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:08:00+00:00Added an answer on June 11, 2026 at 12:08 am

    You don’t return anything from get_post_ids.
    self::get_sub_comments($top_comments); should be self::get_sub_comments($top_comments);?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to grok get a preliminary understanding of monads. I have a
I am trying to grok the output of a function which doesn't have the
I've been trying to grok the org.apache.commons.beanutils library for a method/idiom to evaluate for
I am trying to make my server send gzipped data. I have a grok
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor
trying to figure out why this is happening - I have an input text
I've been trying to parse some huge XML files that LXML won't grok, so
I'm trying to grok gettext . Here's how I think it works - First
I have finally started to take the time out to grok the continuous integration
I'm trying to figure out why the mail function in PHP fails when called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.