通过类的对象访问类中声明的数组


Accessing array declared inside a class via object of that class

我创建了一个名为MEMBERS的类。在这个类中,我将数组声明为成员变量。

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'];
    }
}
}

然后我创建了这个类的对象,如下所示

 $user = new member();

之后,我将函数调用为

$user->connections($user->id);

接下来我要显示数组

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>";
}

这是不工作。我想我的方法是错的。需要一些构造器。但我必须在没有构造函数的情况下做这个。有什么建议吗?

您没有将其分配给class属性,因为您缺少$this->。把它改成这样就可以了:

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