Örneğin yemek odası diye bir ürün var bunun alt ürünleri olarak tv ünitesi vs var bu şekilde bişey yapmak istiyorum

2
●112
class Cart
{
public function addProduct(ProductInterface $product){}
}
class Product implements ProductInterface
{
private $name;
private $price;
public function getName()
{
return $this->name;
}
public function setName(string $name)
{
$this->name = $name;
return $this;
}
public function getPrice()
{
return $this->name;
}
public function setPrice(float $price) // money class kullanılabilir şimdilik float $price olsun.
{
$this->price = $price;
return $this;
}
}
class ProductGroup implements ProductInterface
{
private $name; // group name
private $products = [];
public function addProduct(ProductInterface $product)
{
if (!array_search($product, $this->products)) {
$this->products[] = $product;
}
}
public function removeProduct(ProductInterface $product)
{
if ($key = array_search($product, $this->products)) {
unset($this->products[$key]);
}
}
public function getProducts()
{
return $this->products;
}
//Şimdi product interface de mesela getAmount var diyelim. Burada tek product amount u yerine takımın parasını hesaplaman lazım.
public function getAmount()
{
$amount = 0;
foreach($this->products as $product) {
$amount += $product->getAmount();
}
return $amount;
}
}Şimdi sepete eklerken, kargo hesaplarken vs her yere bu ProductGroup class ını verebilirsin. ProductInterface buralarda kullanacağın methodları içermeli. getAmount gibi. DB den çektiğin takıma ait tüm ürünleri addProduct ile ekleyebilirsin. İstersen removeProduct methodunu kullanarak takımdan istediğin parçayı çıkarabilirsin.