????

Your IP : 216.73.216.34


Current Path : /home/carpe/www/starship/tests/
Upload File :
Current File : /home/carpe/www/starship/tests/objet2.php

<?php

class Personne{
    public $nom;
    public $prenom;
    public $age;
    
    public function __construct($n,$p,$a) {
        $this->nom = $n;
        $this->prenom = $p;
        $this->age = $a;
    }
    
    public function sePresenter(){
        return "Je m'appelle {$this->nom} {$this->prenom}, j'ai {$this->age} ans.";
    }
    
    public function toArray() {
            return [
                'nom' => $this->nom,
                'prenom' => $this->prenom,
                'age' => $this->age,
            ];
        }
}

class Operateur extends Personne{
    public $metier;
    
    public function __construct($n,$p,$a,$m) {
        parent::__construct($n,$p,$a);
        $this->metier = $m;
    }
    
    public function presentation(){
        echo $this->sePresenter();
        return "Je suis {$this->metier}.";
    }
}

class Mentaliste extends Personne{
    public $mana;
    
    public function __construct($n,$p,$a,$m) {
        parent::__construct($n,$p,$a);
        $this->mana = $m;
    }
    
    public function presentation(){
        echo $this->sePresenter();
        return "J'ai {$this->mana} de mana.";
    }
    
    public function infiltration(){
        
    }
    
}

$o1 = new Operateur("Wils","Tony",40,'Pilote');
//echo $o1->presentation();

$personnes = [
    new Operateur("Alice", "Jsp",30,"Medecin"),
    new Operateur("Bob","Lewer", 35,"Agent dentretient"),
    new Mentaliste("Charlie","Chaplin", 28 ,50),
    new Mentaliste("Diana","Fraise", 32, 50)
];

foreach ($personnes as $personne) {
    if ($personne instanceof Operateur) {
        echo $personne->presentation() . " (C'est un opérateur)<br>";
    } elseif ($personne instanceof Mentaliste) {
        echo $personne->presentation() . " (C'est un mentaliste)<br>";
    } else {
        echo $personne->sePresenter() . " (Type inconnu)<br>";
    }
}

?>