Skip to content

Instantly share code, notes, and snippets.

@smwhr
Created March 11, 2019 11:56
Show Gist options
  • Select an option

  • Save smwhr/799d735cd359936825c3cd6fc4287510 to your computer and use it in GitHub Desktop.

Select an option

Save smwhr/799d735cd359936825c3cd6fc4287510 to your computer and use it in GitHub Desktop.
POO 101
<?php
require_once("DoorOpening.php");
class Character{
var $health = 100; //int
var $strength = 5; //int
var $defence = 3; //int
var $age = 1; //int
var $name; //string
protected $category = "neutral"; //string
var $inlife = 1; //bool
var $journey = []; //array
function __construct($name, $category, $birthPlace){
$this->name = $name;
$this->category = $category;
$this->journey[] = $birthPlace;
}
function attack(Character $adversary){
echo $this->name." attaque ".$adversary->name." : ";
if($this->strength > $adversary->defence){
echo "Attaque réussie <br>";
}else{
echo "Attaque échouée <br>";
}
}
function parry(){}
function travelTo($location){
$this->journey[] = $location;
}
function openDoor(DoorOpening $doorOpening){
switch($doorOpening->speed){
case 0:
$adj = "imperceptiblement"; break;
case 1:
$adj = "doucement"; break;
case 2:
$adj = "avec conviction"; break;
case 3:
$adj = "avec force et fracas"; break;
}
echo $this->name." ouvre la porte ".$adj." <br>";
}
function summonSpirit(){
echo $this->name." invoque un esprit ! <br>";
}
function ressuscitate(){
if(in_array($this->category, ["magicien","loup-garou"])){
echo $this->name." revient d'entre les morts ! <br>";
}else{
throw new Exception($this->name." ne peut");
}
}
function transformIntoWolf(){
echo $this->name." se transforme en loup-garou ! <br>";
}
function decret493(){
echo $this->name." passe une loi contre l'avis du Parlement ! <br>";
}
}
<?php
class DoorOpening{
var $speed;//int de 0 à 10
function __construct($speed){
$this->speed = $speed;
}
}
<?php
require_once("Character.php");
require_once("Magicien.php");
require_once("President.php");
require_once("LittleGirl.php");
require_once("DoorOpening.php");
$richard = new Magicien("Richard", "Moscou");
$manudu80 = new President("Emmanuel Macron", "Amiens");
$richard->transformIntoWolf();
//$richard->decret493();
$richard->ressuscitate();
//$manudu80->ressuscitate();
$manudu80->decret493();
$manudu80->summonSpirit();
$manudu80->openDoor(new DoorOpening(2));
$richard->attack($manudu80);
$jeanneyves = new LittleGirl("Jeanne-Yves", "Marseille");
$richard->attack($jeanneyves);
$jeanneyves->attack($richard);
$manudu80->category = "little_girl";
<?php
require_once("Character.php");
require_once("Love.php");
$humbert = new Character("Humbert", "Pedophile", "Paris");
$humbert->age = 50;
$humbert->travelTo("Ramsdale");
$lolita = new Character("Lolita", "Jeune fille", "Ramsdale");
$lolita->age = 12;
$amour = new Love();
try{
$amour->addParticipant($humbert);
$amour->addParticipant($lolita);
}catch(Exception $e){
echo "!!! Une erreur s'est produite :". $e->getMessage()." !!!";
}
echo "<br>la vie continue";
echo "<ul>";
foreach ($amour->participants as $character) {
echo "<li>".$character->name."</li>";
}
echo "</ul>";
<?php
require_once("Character.php");
class LittleGirl extends Character{
function __construct($name, $birthPlace){
parent::__construct($name, "little_girl", $birthPlace);
$this->force = 1;
$this->defence = 198;
}
function attack(Character $character){
throw new Exception("Cannot attack");
}
}
<?php
class Love{
var $numberOfParticipant;
var $participants = []; // Character[]
var $accessoires = []; // string[]
var $position= []; //string
function changePosition(){}
function addParticipant(Character $character){
if($character->age < 16)
throw new Exception($character->name . " is underage");
$this->participants[] = $character;
}
}
<?php
require_once("Character.php");
class Magicien extends Character{
function __construct($name, $birthPlace){
parent::__construct($name, "magicien", $birthPlace);
}
function attack(Character $character){
$original_strength = $this->strength;
if(random_int(0, 1) == 1){
$this->strength = $this->strength * 2;
echo "La force de ".$this->name." double de puissance !<br>";
}
parent::attack($character);
$this->strength = $original_strength;
}
function ressuscitate(){
echo $this->name." revient d'entre les morts ! <br>";
}
}
<?php
class President extends Character{
function __construct($name, $birthPlace){
parent::__construct($name, "president", $birthPlace);
}
function decret493(){
echo $this->name." passe une loi contre l'avis du Parlement ! <br>";
}
}
@s4mking
Copy link

s4mking commented Mar 11, 2019

What a beautiful code.

@noook
Copy link

noook commented Mar 11, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment