-2
我是PHP-POO中的新成員。我想從數組中檢索對象並訪問該對象的屬性。如何從特定對象數組中獲取對象?
我的意圖代碼:
require_once("../modelo/ClubDAO.php");
require_once("../modelo/Club.php");
require_once("../utils/ArrayList.php");
$clubs = new ArrayList();
/* GET ALL THE CLUBS OF THE DATABASE (WORKS GOOD)*/
$clubs = ClubDAO::get_instancia()->getAllClubs();
for($i = 0; $i < $clubs->size(); $i++)
{
$club = new Club();
$club->getNif(); /* HERE I CAN ACCESS TO THE FIELDS */
$club = $clubs->item($i);
$club->/*HERE I CAN'T ACCES TO THE FIELDS OF THE CLASS*/
}
ArrayList類是一個簡單的數組的封裝,對我來說這更容易,這裏的代碼工作:
class ArrayList {
var $array;
public function ArrayList() {
$this->array = array();
}
public function addItem($item){
$this->array[] = $item ;
}
public function toString(){
$cadena = "";
foreach ($this->array as $item) {
$cadena .= $item;
}
return $cadena;
}
public function delete($item){
unset($this->array[$item]);
}
public function item($item){
return $this->array[$item];
}
public function size(){
return count($this->array);
}
}
我不確定你在問什麼......這段代碼不工作?你有錯誤嗎?請讓我們知道你需要幫助。 – Kryten
您正在將'$ clubs'設置爲一個'ArrayList'對象,然後拋棄它並*用'getAllClubs()'替換*。 –