Skip to content

Instantly share code, notes, and snippets.

@krez69
Created November 4, 2019 20:56
Show Gist options
  • Save krez69/f1c748a84bcf5b2d244ad2aa6f653fcd to your computer and use it in GitHub Desktop.
Save krez69/f1c748a84bcf5b2d244ad2aa6f653fcd to your computer and use it in GitHub Desktop.
<?php
abstract class HighWay
{
/**
* @var array
*/
protected $currentVehicules;
/**
* @var int
*/
protected $nbLane;
/**
* @var int
*/
protected $maxSpeed;
public function __construct(int $nbLane, int $maxSpeed)
{
$this->nbLane = $nbLane;
$this->maxSpeed = $maxSpeed;
}
/**
* @return array
*/
public function getCurrentVehicules()
{
return $this->currentVehicules;
}
/**
* @param array $currentVehicules
*/
public function setCurrentVehicules($currentVehicules)
{
$this->currentVehicules = $currentVehicules;
}
/**
* @return int
*/
public function getNbLane()
{
return $this->nbLane;
}
/**
* @param int $nbLane
*/
public function setNbLane($nbLane)
{
$this->nbLane = $nbLane;
}
/**
* @return int
*/
public function getMaxSpeed()
{
return $this->maxSpeed;
}
/**
* @param int $maxSpeed
*/
public function setMaxSpeed($maxSpeed)
{
$this->maxSpeed = $maxSpeed;
}
abstract public function addVehicule(object $vehicle);
}
<?php
require_once 'Bicycle.php';
require_once 'Car.php';
require_once 'Vehicle.php';
require_once 'Truck.php';
require_once 'HighWay.php';
require_once 'MotoWay.php';
require_once 'PedestrianWay.php';
require_once 'ResidentialWay.php';
// Instanciation d'un nouvel objet $k2000
$k2000 = new Car('black',2,'gazol');
$k2000->setNbWheels(4);
$k2000->setCurrentSpeed(5);
// Instanciation d'un nouvel objet $bike
$bike = new Bicycle('blue','2');
$bike->setCurrentSpeed(20);
// Instanciation d'un nouvel objet $renault
$renault = new Truck('red','4','diesel',50,'4');
$renault->setLoadTest(40);
$renault->setCurrentSpeed(3);
// Instanciation d'un nouvel objet $citroen
$citroen = new Vehicle('rouge', '4', '4','diesel');
$motoWay = new MotoWay('4','130');
$pedestrianWay = new PedestrianWay('1','10');
$residentialWay = new ResidentialWay('2', '50');
$motoWay->addVehicule($k2000);
$pedestrianWay->addVehicule($bike);
$residentialWay->addVehicule($citroen);
<?php
require_once 'HighWay.php';
final class MotoWay extends HighWay
{
public function addVehicule(object $vehicle)
{
if ($vehicle instanceof Car){
$this->currentVehicules[] = $vehicle;
}
}
}
<?php
require_once 'HighWay.php';
final class PedestrianWay extends HighWay
{
public function addVehicule( object $vehicle)
{
if ($vehicle instanceof Bicycle){
$this->currentVehicules[] = $vehicle;
}
}
}
<?php
require_once 'HighWay.php';
final class ResidentialWay extends HighWay
{
public function addVehicule(object $vehicle)
{
if ($vehicle instanceof Vehicle){
$this->currentVehicules[] = $vehicle;
}
}
}
<?php
class Vehicle
{
/**
* @var string
*/
protected $color;
/**
* @var integer
*/
protected $currentSpeed;
/**
* @var integer
*/
protected $nbSeats;
/**
* @var integer
*/
protected $nbWheels;
protected $energy;
public function __construct(string $color, int $nbSeats, int $nbWheels, $energy)
{
$this->color = $color;
$this->nbSeats = $nbSeats;
$this->nbWheels = $nbWheels;
$this->energy= $energy;
}
public function forward(): string
{
$this->currentSpeed = 15;
return "Go !";
}
public function brake(): string
{
$sentence = "";
while ($this->currentSpeed > 0) {
$this->currentSpeed--;
$sentence .= "Brake !!!";
}
$sentence .= "I'm stopped !";
return $sentence;
}
public function getCurrentSpeed(): int
{
return $this->currentSpeed;
}
public function setCurrentSpeed(int $currentSpeed): void
{
if ($currentSpeed >= 0) {
$this->currentSpeed = $currentSpeed;
}
}
public function getColor(): string
{
return $this->color;
}
public function setColor(string $color): void
{
$this->color = $color;
}
public function getNbSeats(): int
{
return $this->nbSeats;
}
public function setNbSeats(int $nbSeats): void
{
$this->nbSeats = $nbSeats;
}
public function getNbWheels(): int
{
return $this->nbWheels;
}
public function setNbWheels(int $nbWheels): void
{
$this->nbWheels = $nbWheels;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment