From what I understand you want to know how to make a shopping basket, so I suggest you do something like this:
session_start();
class Imoveis
{
private $favorites = [];
public function addFavorites($product)
{
if (isset($_SESSION['cart'])) {
$this->favorites = unserialize($_SESSION['cart']);
$this->favorites[] = $product;
} else {
$this->favorites[] = $product;
}
$this->setCart();
}
public function removeFavorites($product)
{
if (isset($_SESSION['cart'])) {
$collection = unserialize($_SESSION['cart']);
} else {
$collection = $this->favorites;
}
if (count($collection)) {
foreach ($collection as $key => $productList) {
if ($productList == $product) {
unset($collection[$key]);
}
}
$this->favorites = array_values($collection);
}
$this->setCart();
}
public function setCart()
{
$_SESSION['cart'] = serialize($this->favorites);
}
public function getCart()
{
return unserialize($_SESSION['cart']);
}
}
//instancia o objeto
$imoveis = new Imoveis();
//adiciona
$imoveis->addFavorites('casa 1');
//remove
$imoveis->removeFavorites('casa 1');
//exibe a lista
$imv = $imoveis->getCart();
foreach ($imv as $imovel) {
echo $imovel . nl2br("\n");
}
This is my friend, although I do not master OO, I will try to implement this class. I will give you a feedback to say if I succeeded.
– Igor Silva