How do i modify the quantity within a class object?

时间:2018-02-26 17:52:40

标签: php arrays class object

I've made a cart class and a cartItem class. Every cartItem has an id, quantity and note. I want to be able to add +1 to the quantity of a specifc object when a user clicks on 'add to cart'. But it's not working. I tried something like this: $cart[0]['quantity'] = $cart[0]->setQuanitity($cart[0]->getQuanitity() + 1); Here's the class i've made:

class Cart implements JsonSerializable {
private $cartItems;

public function __construct($cartItems = array()) {
    $this->cartItems = $cartItems;
}

public function addItem(CartItem $cartItem) {
    $this->cartItems[] = $cartItem;
}

public function getItemByIndex($index) {
    return isset($this->cartItems[$index]) ? $this->cartItems[$index] : null;
}

public function getItemsByProductId($productId) {
    $items = array();

    foreach ($this->cartItems as $item) {
        if ($item->getId() === $productId) {
            $items[] = $item;
        }
    }

    return $items;
}

public function getItems() {
    return $this->cartItems;
}

public function setItem($index, $cartItem) {
    if (isset($this->cartItems[$index])) {
        $this->cartItems[$index] = $cartItem;
    }
    return $this;
}

public function removeItem($index) {
    unset($this->cartItems[$index]);
    return $this;
}

public function jsonSerialize() {
    $items = array();
    foreach ($this->cartItems as $item) {
        $items[] = array(
            'id' => $item->getId(),
            'quantity' => $item->getQuantity(),
            'note' => $item->getNote());
    }

    return $items;
}

}

class CartItem {
private $id;
private $quantity;
private $note;

public function __construct($id, $quantity = 0, $note = null) {
    $this->id = $id;
    $this->quantity = $quantity;
    $this->note = $note;
}

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

public function getQuantity() {
    return $this->quantity;
}

public function getNote() {
    return $this->note;
}

public function setId($id) {
    $this->id = $id;
    return $this;
}

public function setQuantity($quantity) {
    $this->quantity = $quantity;
    return $this;
}

public function setNote($note) {
    $this->note = $note;
    return $this;
}

}

Whenever i add a product to the Cart class i want to be able to use the setQuantity and getQuantity functions.

1 个答案:

答案 0 :(得分:1)

You don't need to assign to $cart[0]['quantity']. There are several problems with that:

  1. The elements of $cart are cartItem objects, not associative arrays.
  2. setQuantity() returns $this, not the quantity.
  3. setQuantity() already updates the quantity, you don't need to assign it explicityly.

So just do:

cart[0]->setQuantity($cart[0]->getQuantity() + 1);

If this is frequently needed, you might want to add a method for it, so you don't have to perform two function calls:

private function incQuantity($amount = 1) {
    $this->quantity += $amount;
    return $this;
}

Then you can write:

$cart[0]->incQuantity();