Skip to content

Instantly share code, notes, and snippets.

@sele-nap
Last active March 25, 2022 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sele-nap/6af017c5f6dd19727def67d2d3ba904d to your computer and use it in GitHub Desktop.
Save sele-nap/6af017c5f6dd19727def67d2d3ba904d to your computer and use it in GitHub Desktop.
WCS quest // POO - Basics | Part 1: Faire ses premières classes
<?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;
}
}
}
<?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;
}
}
<?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