Skip to content

Instantly share code, notes, and snippets.

@quandaso
Last active October 10, 2020 04:05
Show Gist options
  • Save quandaso/4e02db042f03979ea3d1438365b3d444 to your computer and use it in GitHub Desktop.
Save quandaso/4e02db042f03979ea3d1438365b3d444 to your computer and use it in GitHub Desktop.
<?php
interface Vehicle {
public function getEngine(): string;
public function simulate();
}
const ENV_LAND = 1;
const ENV_AIR = 2;
const ENV_WATER = 3;
class Transport implements Vehicle
{
protected $engine = 'steam-engine';
protected $name = 'Transport';
protected $toPrint = '##########';
public function __construct($engine = null, $name = null)
{
if ($engine) {
$this->engine = $engine;
}
if ($name) {
$this->name = $name;
}
}
public function getEngine(): string {
echo $this->name . 'Engine: ' . $this->engine ."\n";
return $this->engine;
}
public function simulate(){
$this->getEngine();
echo $this->toPrint."\n";
}
}
class Car extends Transport
{
protected $engine = 'four-stroke engine';
protected $toPrint = '##########';
protected $name = 'Car';
public function driver()
{
}
public function simulate()
{
parent::simulate(); // TODO: Change the autogenerated stub
$this->driver();
}
}
class Aircraft extends Transport
{
protected $name = 'Aircraft';
protected $engine = 'turbofan engine';
protected $toPrint = '>>>>>>>>>>>>>>>';
public function pilot()
{
}
public function simulate()
{
parent::simulate(); // TODO: Change the autogenerated stub
$this->pilot();
}
}
class Submarine extends Transport
{
protected $name = 'Submarine';
protected $engine = 'gasoline engine';
protected $toPrint = '&&&&&&&&&&&&&&';
public function dive()
{
}
public function simulate()
{
parent::simulate(); // TODO: Change the autogenerated stub
$this->dive();
}
}
class Ferrari extends Car
{
protected $name = 'Ferrari';
protected $engine = 'F154';
}
class SuperTransport extends Transport
{
protected $engine = 'hybrid';
protected $name = 'SuperTransport';
private $combineEngines = [
ENV_LAND => Car::class,
ENV_AIR => Aircraft::class,
ENV_WATER => Submarine::class
];
public function simulate($env = null)
{
$class = $this->combineEngines[$env] ?? Car::class;
$instance = new $class($this->engine, $this->name);
$instance->simulate();
}
}
$car = new Car();
$sub = new Submarine();
$pilot = new Aircraft();
$ferrari = new Ferrari();
$car->simulate();
$sub->simulate();
$pilot->simulate();
$ferrari->simulate();
$superTransport = new SuperTransport();
$superTransport->simulate(ENV_LAND);
$superTransport->simulate(ENV_AIR);
$superTransport->simulate(ENV_WATER);
@quandaso
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment