<?
php
class Chien {
// Propriétés
public $nom;
public $race;
// Constructeur pour initialiser les propriétés
public function __construct($nom, $race) {
$this->nom = $nom;
$this->race = $race;
}
// Méthode pour faire aboyer le chien
public function aboyer() {
echo "$this->nom, un chien de race $this->race, aboie :
Ouaf Ouaf!";
}
// Méthode pour afficher les informations du chien
public function afficherInfos() {
echo "Nom : $this->nom, Race : $this->race";
}
}
// Création d'un objet (instance de la classe Chien)
$monChien = new Chien("Rex", "Berger Allemand");
// Appel des méthodes
$monChien->aboyer(); // Affiche "Rex, un chien de race
Berger Allemand, aboie : Ouaf Ouaf!"
echo "<br>";
$monChien->afficherInfos(); // Affiche "Nom : Rex, Race :
Berger Allemand"?>