【问题标题】:Accessing array declared inside a class via object of that class通过该类的对象访问在类中声明的数组
【发布时间】:2023-03-16 14:45:01
【问题描述】:

我创建了一个名为 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>";
}

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

【问题讨论】:

    标签: php arrays oop variables


    【解决方案1】:

    您没有将其分配给类属性,因为您缺少$this-&gt;。把它改成这个,它应该适合你:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多