John Davidson

Php object array (get() method not working)

0 comments
Message:


I'm trying to create an object array but I have several problems.



  • First I can't use $item= array() in ItemModel class function without making it global.

  • Second I`m getting array with some weird values back.


I`m new to programming, can someone explain me what am I doing wrong?


class ItemModel{

private $item= array();

public function setItems(){

global $item;
$test = new Bmw("test....", "BMW", 32, 1, 120);
$item[] = $test;

}

public function getItems(){
global $item;
return $item;

}


}

abstract class Car{

private $id;
private $model;
private $price;
private $carTypeId;

public function __construct($id, $model, $price, $carTypeId){

$this->$id= $id;
$this->$model= $model;
$this->$price = $price;
$this->$carTypeId = $carTypeId;

}

public abstract function getAdditionalInfo();

public function getId(){
return $this->$id;
}
public function getmMdel(){
return $this->$model;
}

}

class Bmw extends Car{


private $weight;

public function __construct($id, $model, $price, $carTypeId, $weight) {

parent::__construct($id, $model, $price, $carTypeId);
$this->$weight= $weight;
}

public function getAdditionalInfo(){

return "Weight: ".$this->$weight;

}


}


class ItemView extends ItemModel{


public function showItems(){

$this->setItems();


foreach ($this->getItems() as $item) {

print_r($item);

}
die;



}

}
$test = new ItemView();
$test->showItems();

Results:
Bmw Object
(
[weight:Bmw:private] =>
[id:Car:private] =>
[model:Car:private] =>
[price:Car:private] =>
[carTypeId:Car:private] =>
[test....] => test....
[BMW] => BMW
[32] => 32
[1] => 1
[120] => 120
)

When I try to use function getId() by changing


foreach ($this->getItems() as $item) {

print_r($item->getId());

}

I get


PHP Warning:  Undefined variable $id in /workspace/Main.php on line 43
PHP Warning: Undefined property: Bmw::$ in /workspace/Main.php on line 43


My Answer:
"test", "1234", "blue");_ $item[] = $test;_ }_ public function getItems(){_ return $this->item;_ }_}_class Bmw{_ private $name;_ private $model;_ private $color;_ public function __construct($name, $model, $color){_ $this->name = $name;_ $this->model = $model;_ $this->color = $color;_ }_}_$itemModel = new ItemModel();_ $itemModel->setItems();_ $items = $itemModel->getItems();_ var_dump($items);_?>_In your code, you are trying to access the $item array without using $this->item in the ItemModel class. This is why you are getting weird values back. You should use $this->item instead of $item in your setItems() method._Also, you don't need to make $item global in your setItems() method. You can directly access the $item property of the class using $this->item._Here is the corrected code:_class ItemModel{_ private $item = array();_ public function setItems(){_ $test = new Bmw("test", "1234", "blue");_ $this->item[] = $test;_ }_ public function getItems(){_ return $this->item;_ }_}_class Bmw{_ private $name;_ private $model;_ private $color;_ public function __construct($name, $model, $color){_ $this->name = $name;_ $this->model = $model;_ $this->color = $color;_ }_}_$itemModel = new ItemModel();_ $itemModel->setItems();_ $items = $itemModel->getItems();_ var_dump($items);_?>_This should work correctly and give you the expected output.

Rate this post

5 of 5 based on 9407 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us