Created
October 15, 2019 13:45
-
-
Save Orianne0605/e2276a12ab71e3bd1d9d1791747c31ee to your computer and use it in GitHub Desktop.
POO Basics 1
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.php | |
class Bicycle | |
{ | |
/** | |
* @param string $color | |
*/ | |
public $color; | |
public function setColor(string $color): void | |
{ | |
$this->color = $color; | |
} | |
/** | |
* @var integer | |
*/ | |
public $currentSpeed; | |
/** | |
* @var integer | |
*/ | |
public $nbSeats = 1; | |
/** | |
* @var integer | |
*/ | |
public $nbWheels = 2; | |
public function forward() | |
{ | |
$this->currentSpeed = 7; | |
return "Go !"; | |
} | |
public function brake(): string | |
{ | |
$sentence = ""; | |
while ($this->currentSpeed > 0) { | |
$this->currentSpeed--; | |
$sentence .= "Brake !!!"; | |
} | |
$sentence .= "I'm stopped !"; | |
return $sentence; | |
} | |
} |
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 | |
{ | |
/** | |
* @var integer | |
*/ | |
private $nbWheels = 4; | |
/** | |
* @var integer | |
*/ | |
private $currentSpeed = 27; | |
/** | |
* @var string | |
*/ | |
private $color = "blue"; | |
/** | |
* @var integer | |
*/ | |
private $nbSeats = 5; | |
/** | |
* @var string | |
*/ | |
private $energy = "gasoil"; | |
/** | |
* @var integer | |
*/ | |
private $energyLevel = 3; | |
/** | |
* Cars constructor. | |
* @param string $color | |
* @param string $nbSeats | |
* @param string $energy | |
*/ | |
public function __construct(string $color, string $nbSeats, string $energy) | |
{ | |
$this->color = $color; | |
$this->nbSeats = $nbSeats; | |
$this->energy = $energy; | |
} | |
public function forward() | |
{ | |
$this->currentSpeed = 16; | |
return "Go !"; | |
} | |
public function brake(): string | |
{ | |
$sentence = ""; | |
while ($this->currentSpeed > 0) { | |
$this->currentSpeed--; | |
$sentence .= "Brake !!!"; | |
} | |
$sentence .= "I'm stopped !"; | |
return $sentence; | |
} | |
/** | |
* @return int | |
*/ | |
public function getNbWheels(): int | |
{ | |
return $this->nbWheels; | |
} | |
/** | |
* @param int $nbWheels | |
*/ | |
public function setNbWheels(int $nbWheels) | |
{ | |
$this->nbWheels = $nbWheels; | |
} | |
/** | |
* @return int | |
*/ | |
public function getCurrentSpeed(): int | |
{ | |
return $this->currentSpeed; | |
} | |
/** | |
* @param int $currentSpeed | |
*/ | |
public function setCurrentSpeed(int $currentSpeed) | |
{ | |
if ($currentSpeed >= 0) { | |
$this->currentSpeed = $currentSpeed; | |
} | |
} | |
/** | |
* @return string | |
*/ | |
public function getColor(): string | |
{ | |
return $this->color; | |
} | |
/** | |
* @return int | |
*/ | |
public function getNbSeats(): int | |
{ | |
return $this->nbSeats; | |
} | |
/** | |
* @return string | |
*/ | |
public function getEnergy(): string | |
{ | |
return $this->energy; | |
} | |
/** | |
* @return int | |
*/ | |
public function getEnergyLevel(): int | |
{ | |
return $this->energyLevel; | |
} | |
/** | |
* @param int $energyLevel | |
*/ | |
public function setEnergyLevel(int $energyLevel) | |
{ | |
$this->energyLevel = $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 | |
require_once 'Cars.php'; | |
require_once 'Bicycle.php'; | |
//Instanciation d'un objet $car | |
$car = new Cars('blue', '5','gasoil'); | |
var_dump($car); | |
// Instanciation d'un nouvel objet $rockrider | |
$rockrider = new Cars('red', '4', 'GPL'); | |
var_dump($rockrider); | |
// Instanciation d'un nouvel objet $tornado | |
$tornado = new Cars('yellow', '2', 'electric'); | |
var_dump($tornado); | |
// Instanciation d'un nouvel objet $bike | |
$bike = new Bicycle (); | |
var_dump($bike); | |
// Moving car | |
echo $car->forward(); | |
echo '<br> Vitesse de la voiture : ' . $car->getCurrentSpeed(). ' km/h' . '<br>'; | |
echo $car->brake(); | |
echo '<br> Vitesse de la voiture : ' . $car->getCurrentSpeed() . ' km/h' . '<br>'; | |
echo $car->brake(); | |
echo '<br>'; | |
// Moving bike | |
echo $bike->forward(); | |
echo '<br> Vitesse du vélo : ' . $bike->currentSpeed . ' km/h' . '<br>'; | |
echo $bike->brake(); | |
echo '<br> Vitesse du vélo : ' . $bike->currentSpeed . ' km/h' . '<br>'; | |
echo $bike->brake(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment