Skip to content

Instantly share code, notes, and snippets.

@vahid-almasi
Created July 4, 2019 09:41
Show Gist options
  • Save vahid-almasi/5b9ec68002817ad6a3f038444612392d to your computer and use it in GitHub Desktop.
Save vahid-almasi/5b9ec68002817ad6a3f038444612392d to your computer and use it in GitHub Desktop.
<?php
interface CarService
{
public function getCost();
}
class BasicInspection implements CarService
{
public function getCost()
{
return 20;
}
}
class OilChange implements CarService
{
protected $car_service;
public function __construct(CarService $car_service)
{
$this->car_service = $car_service;
}
public function getCost()
{
return 19 + $this->car_service->getCost();
}
}
class TireRotation implements CarService
{
protected $car_service;
function __construct(CarService $car_service)
{
$this->car_service = $car_service;
}
public function getCost()
{
return 10 + $this->car_service->getCost();
}
}
echo (new BasicInspection())->getCost(); // just basic inspection = 20
echo (new TireRotation(new OilChange(new BasicInspection())))->getCost(); // basic inspection and oil change and tire rotation = 49
echo (new TireRotation(new BasicInspection()))->getCost(); // basic inspection and tire rotation = 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment