Exemple #1
0
            $this->meat = $this->meat - 2;
        } elseif ($animal->foodType == "herbivore") {
            $this->veggies -= 2;
            //$this->veggies - 2;
        } elseif ($animal->foodType == 'omnivore') {
            $this->veggies -= 2;
            $this->meat -= 2;
        }
    }
    public function getFoodStatus()
    {
        return "There are " . $this->meat . " units of meat, " . $this->veggies . " units of veggies.";
    }
}
$kevin = new Zookeeper(10, 10);
$lion = new Animal('carnivore');
$cow = new Animal('herbivore');
$pig = new Animal('omnivore');
print_r($cow);
echo "<br>";
$kevin->feed($lion);
print_r($kevin);
echo $kevin->getFoodStatus();
echo "<br>";
$kevin->feed($cow);
echo $kevin->getFoodStatus();
echo "<br>";
$kevin->feed($pig);
echo $kevin->getFoodStatus();
echo "<br>";
print_r($kevin);
Exemple #2
0
    private $veggies = 10;
    public function feed($animal)
    {
        if ($animal->foodType == 'carnivore') {
            $this->meat -= 2;
        } elseif ($animal->foodType == 'herbivore') {
            $this->veggies -= 2;
        } elseif ($animal->foodType == 'omnivore') {
            $this->meat--;
            $this->veggies--;
        } else {
            die("Invalid animal type: " . $animal->foodType);
        }
    }
    public function getFoodStatus()
    {
        return "meat: " . $this->meat . "veggies: " . $this->veggies;
    }
}
$lion = new Animal('carnivore');
$sloth = new Animal('herbivore');
$bear = new Animal('omnivore');
$lorax = new Animal('candivore');
$zookeeper = new Zookeeper();
echo $zookeeper->getFoodStatus(), "<br>";
$zookeeper->feed($lion);
echo $zookeeper->getFoodStatus(), "<br>";
$zookeeper->feed($sloth);
echo $zookeeper->getFoodStatus(), "<br>";
$zookeeper->feed($bear);
echo $zookeeper->getFoodStatus(), "<br>";