开发者

Accessing array declared inside a class via object of that class

开发者 https://www.devze.com 2023-04-08 16:39 出处:网络
I have created a class named as MEMBERS. In this class I have declared an array as a member variable.

I have created a class named as MEMBERS. In this class I have declared an array as a member variable.

class member
{
public $arr_connections;
function connections($id)
{
    $query = mysql_query("Select * from connections where user_id = '$id'");
    while($info = mysql_fetch_array($query))
    {
        $arr_connections[] = $info['connection_id'];
    }

}
}

Then I have created an object of this class as follow

 $user = new member();

After this I am calling the function as

$user->connections($开发者_运维百科user->id);

Next I am displaying the array

foreach($user->arr_connections as $mem_id)
{
            echo $mem_id;
            $person = new member($mem_id);
            echo "<a href = 'profile.php?id=$person->id'><img src = '$person->display_picture'/ width = 30 height = 30></a>";
}

This is not working. I guess my method is wrong. Some constructor was required. But I have to do this without constructor. Any suggestions?


You're not assigning it to the class property, because you're missing $this->. Change it into this and it should work for you:

$this->arr_connections[] = $info['connection_id'];
0

精彩评论

暂无评论...
验证码 换一张
取 消