Skip to content

Instantly share code, notes, and snippets.

@clarade
Created March 24, 2021 21:58
Show Gist options
  • Save clarade/25db5ec9c36a0ffbbdc45b8c66781f6e to your computer and use it in GitHub Desktop.
Save clarade/25db5ec9c36a0ffbbdc45b8c66781f6e to your computer and use it in GitHub Desktop.
Exercise about abstract and final classes in OOP
<?php
abstract class HighWay
{
protected array $currentVehicles;
protected int $nbLane;
protected int $maxSpeed;
/**
* Get the value of currentVehicles
*
* @return array
*/
public function getCurrentVehicles()
{
return $this->currentVehicles;
}
/**
* Set the value of currentVehicles
*
* @param array $currentVehicles
*
* @return self
*/
public function setCurrentVehicles(array $currentVehicles)
{
$this->currentVehicles = $currentVehicles;
return $this;
}
/**
* Get the value of nbLane
*
* @return int
*/
public function getNbLane()
{
return $this->nbLane;
}
/**
* Set the value of nbLane
*
* @param int $nbLane
*
* @return self
*/
public function setNbLane(int $nbLane)
{
$this->nbLane = $nbLane;
return $this;
}
/**
* Get the value of maxSpeed
*
* @return int
*/
public function getMaxSpeed()
{
return $this->maxSpeed;
}
/**
* Set the value of maxSpeed
*
* @param int $maxSpeed
*
* @return self
*/
public function setMaxSpeed(int $maxSpeed)
{
$this->maxSpeed = $maxSpeed;
return $this;
}
abstract public function addVehicle(Vehicle $vehicle) : void;
}
<?php
require_once '../src/Bicycle.php';
require_once '../src/Car.php';
require_once '../src/Truck.php';
require_once '../src/MotorWay.php';
require_once '../src/PedestrianWay.php';
require_once '../src/ResidentialWay.php';
$bicycle = new Bicycle('blue', 1, 'human energy');
echo $bicycle->forward();
// var_dump($bicycle);
$car = new Car('green', 4, 'electric');
echo $car->forward();
var_dump($car);
var_dump(Car::ALLOWED_ENERGIES);
// Showing the steps of the truck's filling
$truck = new Truck('red', 2, 50);
// echo "<strong> The truck is </strong>" . $truck->filling();
// Getting the departure, currentspeed of the truck
$truck2 = new Truck('black', 3, 40);
// echo "<strong> The second truck starts... </strong>" . $truck2->forward() . "</br>" . $truck2->getCurrentSpeed() ."'s the current speed... </br>" . $truck2->brake();
// Creating a new electric car with Car type
$electricCar = new Car('green', 4, 'electric');
// var_dump($electricCar);
// Creating a new bikecar with Bicycle type
$bTwin = new Bicycle('orange', 2, 'human');
// Creating a new Motorway called Vinci
$vinciAutoroute = new MotorWay();
var_dump($vinciAutoroute);
// Adding a new vehicle with a $bTwin bicycle type to our Motorway
$vinciAutoroute->addVehicle($bTwin);
var_dump($vinciAutoroute);
// Adding a new vehicle on $VinciAutoroute
$vinciAutoroute->addVehicle($electricCar);
var_dump($vinciAutoroute);
// Creating a new Residentialway called $residentialSquare
$residentialSquare = new ResidentialWay();
$residentialSquare->addVehicle($truck2);
var_dump($residentialSquare);
// Creating a new Pedestrianway called $pedestrianSquare
$pedestrianSquare = new PedestrianWay();
// Adding a new vehicle with a $bTwin bicycle type to our Pedestrianway
$pedestrianSquare->addVehicle($bTwin);
var_dump($pedestrianSquare);
// Adding a new vehicle with an $electricCar type to our Pedestrianway
$pedestrianSquare->addVehicle($electricCar);
var_dump($pedestrianSquare);
<?php
require_once 'Highway.php';
final class MotorWay extends HighWay
{
public function __construct()
{
$this->nbLane = 4;
$this->maxSpeed = 130;
}
public function addVehicle(Vehicle $vehicle) : void
{
if (is_a($vehicle, 'Car')) {
$this->currentVehicles[] = $vehicle;
} else {
echo "<strong> Ce véhicule n'est pas une voiture! </strong>";
}
}
}
<?php
require_once 'Highway.php';
final class PedestrianWay extends HighWay
{
public function __construct()
{
$this->nbLane = 1;
$this->maxSpeed = 10;
}
public function addVehicle(Vehicle $vehicle) : void
{
if (is_a($vehicle, 'Bicycle') || is_a($vehicle, 'Skate')) {
$this->currentVehicles[] = $vehicle;
} else {
echo "<strong> Ce véhicule n'est pas un vélo ou un skate! </strong>";
}
}
}
<?php
require_once 'Highway.php';
final class ResidentialWay extends HighWay
{
public function __construct()
{
$this->nbLane = 2;
$this->maxSpeed = 50;
}
public function addVehicle(Vehicle $vehicle) : void
{
$this->currentVehicles[] = $vehicle;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment