我有,我想分析從SQL-行輸入對象的方法:試圖遍歷對象數組PHP
function buildPictureArray($sqlRow) {
$pics = [];
foreach($sqlRow as $value) {
extract($value); // How to do this more elegant?
$newObj = new PICObject($id, $filename, $thumb . $filename, $full . $filename, $title, $subtitle); // These are column names I got from extract
$pics[] = $newObj;
}
return $pics;
}
我PICObject看起來是這樣的:
Class PICObject {
public $id;
public $fileName;
public $thumbLink;
public $fullLink;
public $title;
public $subTitle;
function __construct($id, $fileName, $thumbLink, $fullLink, $title, $subTitle) {
$this->$id = $id;
$this->$fileName = $fileName;
$this->$thumbLink = $thumbLink;
$this->$fullLink = $fullLink;
$this->$title = $title;
$this->$subTitle = $subTitle;
}
}
現在理想情況下,我想迭代這個$ pics數組,它應該包含多個PICObjects並能夠訪問其字段:
foreach($pics as $picObj) {
echo $picObj->$fileName;
}
Howe ver,這不起作用。那麼如何做到這一點? (我已經嘗試了多種方法,但沒有真正做了什麼,我一直在尋找)
檢查喲你的語法。 '$ this-> id'不是'$ this - > $ id','echo $ picObj-> fileName;''不是'echo $ picObj - > $ fileName;' – check