- var_dump: documentation ( source)
- json_decode: documentation ( source)
- json_encode: documentation ( source)
<?php
//EXAMPLE, PSEUDO CODE
class Cart{
protected $items = [];//array of id => [stuff]
public function __construct(){
$this->read();
}
//$attributes - IDK, maybe quantity or ... something...
public function add(int $id, array $attributes = []){
if(isset($this->items [$id])){
//@todo - do smth about it...
}
$this->items [$id][] = $attributes;
return $this;
}
public function read(){
//might change this later, to db and or serialization
$this->items = json_decode((isset($_SESSION['cart'])));
return $this;
}
public function write(){
//might change this later, to db and or serialization
$_SESSION['cart'] = json_encode($this->items);
return $this;
}
public function getItems(){
return $this->items;
}
}
///
//thing is you don't care HERE about internal implementaion. Which can be changed later.
$cart = new Cart();
$cart->add(17,['quantity' => 12]);
$cart->add(42,['quantity' => 99])->write();
var_dump($cart->getItems());