1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| <?php
class Animal
{
private $_nom; // le nom de l'animal
private $_position; // sa position dans la rue
public function Mouvement($pas, $animaux)
{
while ($pas > 0)
{
$this->_position ++;
// un animal est-il présent dans la case d'arrivé ?
foreach ($animaux as $animal)
{
$animal_position = $animal->Position();
// si oui on lui dit bonjour
if ($this->_position == $animal_position && $this != $animal)
{
echo $this->Bonjour($animal);
}
}
$pas --;
}
}
public function Position()
{
return $this->_position;
}
protected function Bonjour($animal)
{
$texte = 'Bonjour '.get_class($animal);
return $texte;
}
public function __construct($nom, $position)
{
$this->_nom = $nom;
$this->_position = $position;
}
}
class Chat extends Animal
{
protected function Bonjour($animal)
{
$type = get_class($animal);
switch($type)
{
case 'Chien':
$texte = 'Le chat fuit, il a peur du chien';
break;
default:
$texte = 'Bonjour '.$type;
break;
}
return $texte;
}
}
class Chien extends Animal
{
}
$medor = new Chien('medor', 4);
$catzie = new Chat('catzie', 1);
$rue = array($medor, $catzie);
$catzie->Mouvement(5, $rue);
echo '<br />';
$medor->Mouvement(5, $rue); |