I am having an issue with a ShoppingCart object that I am using on a website to store information about menu items that customers add to their personal shopping cart.
I am relatively new to PHP, but have been coding in Java for close to 10 years, so I have a firm understanding on how object oriented programming works.
In my shopping cart I have two instance variables that are declared as follows:
public $items;
public $count;
public function __construct(){
$items=array();
$count=0;
}
Where items is supposed to be an array to store CartItem objects.
I insert an item into the array as follows:
public function addToCart($item_id, $quantity){
$newItem=new CartItem();
$newItem->setID($item_id);
$newItem->setQuant($quantity);
$this->$items[]=$newItem;
$this->$count++;
}
Now I am getting the following warning whenever I try to run:
Warning: Cannot use a scalar value as an array
Where the warning points to the line
$this->$items[]=$newItem;
I am having some troubles fully understanding this warning, and understanding what I have done wrong. I am not sure exactly what this warning is telling me, but I feel like it is recognizing $items as a scalar value, because whenever I try to echo it, I get a printed value of 0 rather than Array.