0

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.

backward forward
  • 429
  • 3
  • 17
  • what do you get if you do `var_dump($this)` add the beginning of `addToChart` function ? – Ôrel Nov 07 '20 at 10:01
  • 2
    _Side note:_ You can give the properties default values when defining them instead of in the constructor: `public $items = [];` and `public $count = 0;`. Then you don't even need that constructor, if setting defaults is all it does. – M. Eriksson Nov 07 '20 at 10:06
  • 4
    `$this->$items` should be `$this->items` as should `$this->count` – Nick Nov 07 '20 at 10:08
  • I don't doubt that you know Java, but PHP isn't Java. I would recommend you to check out the manual about classes and objects: https://www.php.net/manual/en/language.oop5.php – M. Eriksson Nov 07 '20 at 10:11
  • Does this answer your question? [PHP - cannot use a scalar as an array warning](https://stackoverflow.com/questions/6019853/php-cannot-use-a-scalar-as-an-array-warning) – Tigger Nov 07 '20 at 10:28
  • @Ôrel object(ShoppingCart)#1 (3) { ["items"]=> NULL ["count"]=> NULL [""]=> int(0) } -1 – backward forward Nov 07 '20 at 10:28

2 Answers2

2
$this->items[]=$newItem;
$this->count++;
Phindette
  • 33
  • 4
2

To initialize your variables in your constructor you must reference them by doing

$this->items = array();

or

$this->items = [];

instead of just typing

$items = array();

The second part, what is mentioned in the comments, is that

$this->$items;

is incorrect, and should be

$this->items;

(without the second $)

But, like also mentioned, if all you do is assign a default value / initialize the variables, you can also do

class ShoppingCart {
    public $items = array();
    public $count = 0;
}

such that a constructor is not necessary in this situation.

AntiFTW
  • 41
  • 8
  • An improvement though, there's no reason to even have a constructor in this case since you can set the defaults directly when defining the properties. – M. Eriksson Nov 07 '20 at 10:34
  • 1
    True, but I thought this would explain the problem instead of suggesting another solution – AntiFTW Nov 07 '20 at 10:40