Skip to content

Instantly share code, notes, and snippets.

@quandaso
Last active October 10, 2020 12:44
Show Gist options
  • Save quandaso/a6b7091ad9622abe4a675601adb5a2ac to your computer and use it in GitHub Desktop.
Save quandaso/a6b7091ad9622abe4a675601adb5a2ac to your computer and use it in GitHub Desktop.
<?php
interface Vehicle {
public function getEngine(): string;
public function simulate();
}
function getSubclassesOf($parent) {
$result = array();
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, $parent))
$result[] = $class;
}
return $result;
}
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 SuperTransport extends Transport
{
protected $engine = 'hybrid';
protected $name = 'Verhicle';
public function getOtherTransport()
{
$classes = getSubclassesOf(Transport::class);
$instances = [];
foreach ($classes as $class) {
if ($class !== 'SuperTransport') {
$instances[] = new $class();
}
}
return $instances;
}
public function simulate()
{
$this->getEngine();
foreach ($this->getOtherTransport() as $instance) {
$instance->simulate();
}
}
}
$car = new Car();
$sub = new Submarine();
$pilot = new Aircraft();
$superTransport = new SuperTransport();
echo "1.\n";
$superTransport->simulate();
echo "2.\n";
$car->simulate();
echo "3.\n";
$sub->simulate();
echo "4.\n";
$pilot->simulate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment