似乎找不到这个问题的答案:如何从对象数组中获取特定值(成员值)?
我的代码很简单:
$people = array();
class Person {
public $id;
public $name;
public $family_name;
public $dob;
public $image;
public function __construct($id, $name, $family_name, $dob, $image){
$this->$id = (string) $id;
$this->$name = (string) $name;
$this->$family_name = (string) $family_name;
$this->$dob = (string) $dob;
$this->$image = (string) $image;
}
public function get_id(){
return $this->id;
}
}
for ($i=0;$i<$no_clients;$i++)
{
array_push($people, new Person($_SESSION['user_clients'][$i]['client_id'], $_SESSION['user_clients'][$i]['client_name'], $_SESSION['user_clients'][$i]['client_family_name'], $_SESSION['user_clients'][$i]['client_dob'], ROOT_URL.$_SESSION['user_clients'][$i]['client_img']));
}
现在我想从 people 数组中获取其中一个人的 id
$error = $people[$i]->get_id(); //doesn't seem to work
//not getting a value back even though the session variable is correct
正如您可能已经看到的,我是 PHP 新手,所以任何建议都会很棒。
谢谢