Skip to content

Instantly share code, notes, and snippets.

@clarade
Created March 21, 2021 14:02
Show Gist options
  • Save clarade/3df8f6a3b57d7682fbe4d9078267a955 to your computer and use it in GitHub Desktop.
Save clarade/3df8f6a3b57d7682fbe4d9078267a955 to your computer and use it in GitHub Desktop.
Exercise about POO heritage and parents composants
<?php
require_once 'Vehicle.php';
class Bicycle extends Vehicle
{
public function __construct(string $color, int $nbSeats, string $energy)
{
parent::__construct($color, $nbSeats);
$this->energy = $energy;
}
}
<?php
require_once 'Vehicle.php';
class Car extends Vehicle
{
const ALLOWED_ENERGIES = ['fuel', 'electric'];
protected $energy;
protected $energyLevel;
public function __construct(string $color, int $nbSeats, string $energy)
{
parent::__construct($color, $nbSeats);
$this->energy = $energy;
}
public function getEnergy(): string
{
return $this->energy;
}
public function setEnergy(string $energy): Car
{
if (in_array($energy, self::ALLOWED_ENERGIES)) {
$this->energy = $energy;
}
return $this;
}
public function getEnergyLevel(): int
{
return $this->energyLevel;
}
public function setEnergyLevel(int $energyLevel): void
{
$this->energyLevel = $energyLevel;
}
}
<?php
require_once '../src/Bicycle.php';
require_once '../src/Car.php';
require_once '../src/Truck.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);
$truck = new Truck('red', 2, 50);
echo "<strong> The truck is </strong>" . $truck->filling();
$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();
<?php
class Truck extends Vehicle
{
private $stockageCapacity;
private $fillingTruck;
public function __construct(string $color, int $nbSeats, int $stockageCapacity, int $fillingTruck = 0)
{
parent::__construct($color, $nbSeats);
$this->stockageCapacity = $stockageCapacity;
$this->fillingTruck = $fillingTruck;
}
public function filling()
{
$sentence = "";
while ($this->fillingTruck < $this->stockageCapacity) {
$this->fillingTruck++;
$sentence .= $this->isFull() . "</br>";
}
return $sentence;
}
public function isFull() : string
{
if ($this->fillingTruck >= $this->stockageCapacity) {
return "Full!";
} else {
return "in filling..";
}
}
public function getStockageCapacity()
{
return $this->stockageCapacity;
}
public function setStockageCapacity($stockageCapacity)
{
$this->stockageCapacity = $stockageCapacity;
return $this;
}
public function getFillingTruck()
{
return $this->fillingTruck;
}
public function setFillingTruck($fillingTruck)
{
$this->fillingTruck = $fillingTruck;
return $this;
}
}
<?php
// Vehicle.php
class Vehicle
{
/**
* @var string
*/
protected $color;
/**
* @var integer
*/
protected $currentSpeed;
/**
* @var integer
*/
protected $nbSeats;
/**
* @var integer
*/
protected $nbWheels;
public function __construct(string $color, int $nbSeats)
{
$this->color = $color;
$this->nbSeats = $nbSeats;
}
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