Last active
March 25, 2022 11:11
-
-
Save sele-nap/6af017c5f6dd19727def67d2d3ba904d to your computer and use it in GitHub Desktop.
WCS quest // POO - Basics | Part 1: Faire ses premières classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Bicycle | |
{ | |
private $color; | |
private $currentSpeed; | |
private $nbSeats = 1; | |
private $nbWheels = 2; | |
//////////////////////////////////////////////////////////////////// | |
public function forward() | |
{ | |
$this->currentSpeed; | |
return "Go !"; | |
} | |
public function brake(): string | |
{ | |
$sentence = ""; | |
while ($this->currentSpeed > 0) { | |
$this->currentSpeed--; | |
$sentence .= "Brake !!!"; | |
} | |
$sentence .= "I'm stopped !"; | |
return $sentence; | |
} | |
public function getColor(): string | |
{ | |
return $this->color; | |
} | |
public function setColor(string $color): void | |
{ | |
$this->color = $color; | |
} | |
public function __construct(string $color, int $currentSpeed) | |
{ | |
$this->color = $color; | |
$this->currentSpeed = $currentSpeed; | |
} | |
public function getCurrentSpeed(): int | |
{ | |
return $this->currentSpeed; | |
} | |
public function setCurrentSpeed(int $currentSpeed): void | |
{ | |
if ($currentSpeed >= 0) { | |
$this->currentSpeed = $currentSpeed; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Cars | |
{ | |
private $color; | |
private $currentSpeed; | |
private $nbSeats; | |
private $nbWheels; | |
private $energy; | |
private $energyLevel; | |
//////////////////////////////////////////////////////////////////// | |
public function __construct(string $color, int $nbSeats, string $energy) | |
{ | |
$this->color = $color; | |
$this->nbSeats = $nbSeats; | |
$this->energy = $energy; | |
} | |
public function forward() | |
{ | |
$this->currentSpeed; | |
return "Go !"; | |
} | |
public function brake(): string | |
{ | |
$sentence = ""; | |
while ($this->currentSpeed > 0) { | |
$this->currentSpeed--; | |
$sentence .= "Brake !!!"; | |
} | |
$sentence .= "I'm stopped !"; | |
return $sentence; | |
} | |
public function start() | |
{ | |
$this->currentSpeed = 20; | |
} | |
public function getNbWheels(): int | |
{ | |
return $this->nbWheels; | |
} | |
public function getCurrentSpeed(): int | |
{ | |
return $this->currentSpeed; | |
} | |
public function getColor(): string | |
{ | |
return $this->color; | |
} | |
public function getNbSeats(): string | |
{ | |
return $this->nbSeats; | |
} | |
public function getEnergy(): string | |
{ | |
return $this->energy; | |
} | |
public function getEnergyLevel(): string | |
{ | |
return $this->energyLevel; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//////////////////////////// BICYCLE /////////////////////////// | |
require_once 'Bicycle.php'; | |
// Instanciation d'un nouvel objet $bike avec couleur et vitesse (-construct) | |
$bike = new Bicycle("purple", 15); | |
// Moving rockrider | |
echo $bike->forward(); | |
echo '<br> Vitesse du vélo : ' . $bike->getCurrentSpeed() . ' km/h' . '<br>'; | |
echo $bike->brake(); | |
echo '<br> Vitesse du vélo : ' . $bike->getcurrentSpeed() . ' km/h' . '<br>'; | |
echo $bike->brake() . '<br>'; | |
// Instanciation d'un nouvel objet $rockrider avec couleur et vitesse (-construct) | |
$rockrider = new Bicycle("yellow", 10); | |
// Moving rockrider | |
echo $rockrider->forward(); | |
echo '<br> Vitesse du vélo : ' . $rockrider->getCurrentSpeed() . ' km/h' . '<br>'; | |
echo $rockrider->brake(); | |
echo '<br> Vitesse du vélo : ' . $rockrider->getCurrentSpeed() . ' km/h' . '<br>'; | |
echo $rockrider->brake() . '<br>'; | |
// Instanciation d'un nouvel objet $tornado avec couleur et vitesse (-construct) | |
$tornado = new Bicycle("red", 8); | |
// Moving tornado | |
echo $tornado->forward(); | |
echo '<br> Vitesse du vélo : ' . $tornado->getCurrentSpeed() . ' km/h' . '<br>'; | |
echo $tornado->brake(); | |
echo '<br> Vitesse du vélo : ' . $tornado->getCurrentSpeed() . ' km/h' . '<br>'; | |
echo $tornado->brake() . '<br>'; | |
//////////////////////////// CARS ////////////////////////////// | |
require_once 'Cars.php'; | |
// Instanciation d'un nouvel objet $mini avec couleur et nb sièges et energy (-construct) | |
$mini = new Cars("yellow", 5, "electric"); | |
// Moving mini | |
echo $mini->start(); | |
echo '<br> Démarrage ok' . '<br>'; | |
echo $mini->forward(); | |
echo '<br> Vitesse de la voiture : ' . $mini->getCurrentSpeed() . ' km/h' . '<br>'; | |
echo $mini->brake(); | |
echo '<br> Vitesse de la voiture : ' . $mini->getCurrentSpeed() . ' km/h' . '<br>'; | |
echo $mini->brake(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment