- Output for 8.3.25
- Reving normal Reving normal Reving to the red line!
<?php
declare(strict_types = 1);
class Engine
{
public function rev(): void
{
echo "Reving normal\n";
}
}
class SportEngine extends Engine
{
public function revHigh(): void
{
echo "Reving to the red line!\n";
}
}
class Car
{
protected Engine $engine;
public function __construct(Engine $engine)
{
$this->engine = $engine;
}
public function getEngine(): Engine
{
return $this->engine;
}
}
class SportsCar extends Car
{
public function __construct(SportEngine $engine)
{
$this->engine = $engine;
}
public function getSportEngine(): SportEngine
{
return $this->engine;
}
}
$normalCar = new Car(new Engine);
$sportScar = new SportsCar(new SportEngine());
$normalCar->getEngine()->rev();
$sportScar->getEngine()->rev();
$sportScar->getSportEngine()->revHigh();