Last active
January 23, 2019 15:19
-
-
Save CyberSecutor/80b9e1348bde04d367e0fbbd7280f648 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Engine | |
{ | |
private $pk; | |
public function __construct(int $pk) | |
{ | |
$this->pk = $pk; | |
} | |
} | |
interface ElectricVehicleInterface { | |
public function isPluginHybrid(): bool; | |
} | |
interface CombustionVehicleInterface {} | |
interface VehicleInterface | |
{ | |
public function getEngine(): \Engine; | |
public function setEngine(\Engine $engine); | |
} | |
abstract class AbstractAuto implements Vehicle | |
{ | |
protected $engine; | |
public function getEngine(): \Engine | |
{ | |
return $this->engine; | |
} | |
abstract function setEngine(\Engine $engine): void; | |
} | |
class Fiat extends AbstractAuto implements ElectricVehicleInterface | |
{ | |
private $isHybrid = false; | |
public function __construct(bool $isHybrid) | |
{ | |
$this->isHybrid; | |
} | |
public function getEngine(): \Engine | |
{ | |
return $this->engine; | |
} | |
public function isPluginHybrid(): bool | |
{ | |
return $this->isHybrid; | |
} | |
public function setEngine(\Engine $engine) | |
{ | |
$this->engine = $engine; | |
} | |
} | |
class FordMustang extends AbstractAuto implements CombustionVehicleInterface | |
{ | |
public function setEngine(\Engine $engine) | |
{ | |
$this->engine = "$engine x 2"; | |
} | |
} | |
class Garage | |
{ | |
private $vehicles; | |
public function __construct() | |
{ | |
$this->vehicles = array(); | |
} | |
public function addVehicle(\VehicleInterface $auto) | |
{ | |
if ($auto instanceof \ElectricVehicleInterface) { | |
// Do discount. | |
if ($auto->isHybrid()) { | |
// Do less discount. | |
} | |
} | |
$this->vehicles[] = $auto; | |
} | |
} | |
$fiat = new Fiat(); | |
$fiat->setEngine(new Engine(10)); | |
$mustang = new FordMustang(); | |
$mustang->setEngine(new Engine(400)); | |
$garage = new Garage(); | |
$garage->addVehicle($fiat); | |
$garage->addVehicle($mustang); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment