Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface ShopInterface
- {
- public function getTotalPrice(): float;
- }
- interface ProductInterface
- {
- public function getPrice(): float;
- }
- class Shop implements ShopInterface
- {
- private $products;
- public function __construct(array $products)
- {
- $this->products = $products;
- }
- public function getTotalPrice(): float
- {
- $totalPrice = 0;
- foreach ($this->products as $product) {
- $totalPrice+=$product->getPrice();
- }
- return $totalPrice;
- }
- }
- class Product implements ProductInterface
- {
- private $price;
- public function __construct(float $price)
- {
- $this->price = $price;
- }
- public function getPrice(): float
- {
- return $this->price;
- }
- }
- $apple = new Product(1.99);
- $banana = new Product(2.99);
- $lemon = new Product(3.99);
- $shop = new Shop([$apple, $banana, $lemon]);
- echo $shop->getTotalPrice() . PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement