2

I tried to get followers from MySQL usingy this class

class get_followers {
    public $followers_arr = array();
    public function __construct($user_id) {
        $query = "select * from followsystem where following ='$user_id'";

        $q = mysql_query($query) or die(mysql_error());

        $count = mysql_num_rows($q);

        if ($count > 0) {
            while ($row = mysql_fetch_assoc($q)) {
                array_push($this->followers_arr, $row['userid']);
           }
        }

        return $this->followers_arr;
    }
}

Then I initialize this class

$fol = new get_followers($userid);
$fol_arr = json_encode($fol);
echo $fol_arr;

Then I get

{"followers_arr":["1234","456"]}

but what i want want just to get this

["1234","456"]

How is that works?

4
  • Do not json_encode($fol); Commented Jan 23, 2015 at 11:14
  • Try: print_r($fol->followers_arr); and remove the $fol_arr = json_encode($fol); just to: $fol_arr = $fol; Commented Jan 23, 2015 at 11:14
  • First of all it's not OOP even though you are creating object. Read some more about that. Commented Jan 23, 2015 at 11:14
  • $fol = new get_followers($userid); $fol_arr = json_encode($fol->followers_arr); echo $fol_arr; it works when i did this thank you all Commented Jan 23, 2015 at 11:20

2 Answers 2

3

I don't think you understand how constructors work. You can't return a value from a constructor because it's just used to instantiate the object. When you're doing $fol_arr = json_encode($fol); you're actually encoding the entire object, not it's return value.

If you really want to use a class to do this, you should add a method to the class and use that, like this:

class Followers {
    public $followers_arr = array();
    public $user_id = null;

    public function __construct($user_id) {
        $this->user_id = $user_id;            
    }

    public function get()
    {
        $query = "select * from followsystem where following ='{$this->user_id}'";

        $q = mysql_query($query) or die(mysql_error());

        $count = mysql_num_rows($q);

        if ($count > 0) {
            while ($row = mysql_fetch_assoc($q)) {
                array_push($this->followers_arr, $row['userid']);
           }
        }

        return $this->followers_arr;
    }
}

And use it like this:

$fol = new Followers($userid);
$fol_arr = json_encode($fol->get());
echo $fol_arr;
Sign up to request clarification or add additional context in comments.

Comments

0

The solution to your problem is to do $fol_arr = json_encode($fol->followers_arr);

Nonetheless, making a class in this case is completely obsolete, since you only make it as a wrapper for a single function you want to execute (called get_followers) Instead of making a class, you could simply make the following:

function get_followers($user_id) {
        $followers_arr = [];
        $query = "select * from followsystem where following ='$user_id'";

        $q = mysql_query($query) or die(mysql_error());

        $count = mysql_num_rows($q);

        if ($count > 0) {
            while ($row = mysql_fetch_assoc($q)) {
                array_push($followers_arr, $row['userid']);
           }
        }

        return $followers_arr;

}

$fol = get_followers($userid);
$fol_arr = json_encode($fol);
echo $fol_arr;

There is no need to put it in a class unless the class serves the purpose of combining a few functions and variables to create a behaviour.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.