Esempio n. 1
0
    public function display_hp()
    {
        echo '----------------------';
        echo '<br>';
        echo $this->name;
        echo '<br>';
        echo $this->hp;
        echo '<br>';
    }
}
//---------END OF ANIMAL CLASS CODE---------//
$animal1 = new Animal('animal');
$animal1->walk();
$animal1->walk();
$animal1->walk();
$animal1->run();
$animal1->run();
$animal1->display_hp();
//---------------DOG CLASS------------------//
class Dog extends Animal
{
    public function __construct($name)
    {
        $this->name = $name;
        $this->hp = 150;
    }
    public function pet()
    {
        $this->hp += 5;
    }
}
Esempio n. 2
0
        } else {
            throw new ERR_NOT_FOUND("Sorry, the barn does not contain {$args['0']}.");
        }
        $resp->status()->add_message("{$args['0']} is now dead. Good job!");
        return Worker::WORKER_SUCCESS;
    }
    function do_work($id, $resp)
    {
        $animal = $this->get_animal($id);
        if ($animal['species'] == 'Araneus cavaticus') {
            $this->spin_web($animal['name'], $resp);
        } else {
            $resp->status()->add_message($animal['name'] . " is happily rolling around in the mud!");
        }
    }
    function spin_web($name, $resp)
    {
        $resp->status()->progress(10);
        $resp->status()->add_message("{$name} is beginning to spin a web.");
        sleep(30);
        $resp->status()->progress(50);
        $resp->status()->add_message("{$name} is still spinning...");
        sleep(30);
        $messages = array('SOME PIG', 'TERRIFIC', 'RADIANT', 'HUMBLE');
        $message = $messages[array_rand($messages)];
        $resp->status()->add_message("{$name}'s web is complete! It says: '{$message}'");
    }
}
$worker = new Animal($argv[1]);
$worker->run();
Esempio n. 3
0
$animal_one->sound = "Ruff";
// The statements $animal_one->att_name call __get
// We call static attributes like this Class::$static_var
echo $animal_one->name . " says " . $animal_one->sound . " give me some " . $animal_one->favorite_food . " my id is " . $animal_one->id . " total animals = " . Animal::$number_of_animals . "<br /><br />";
// If we defined a constant in the class we would get its
// value like this Class::CONTANT
echo "Favorite Number " . Animal::PI . "<br />";
$animal_two = new Dog();
$animal_two->name = "Grover";
$animal_two->favorite_food = "Mushrooms";
$animal_two->sound = "Grrrrrrr";
// Even though we are referring to the Dog $number_of_animals it
// still increases even with subclasses
echo $animal_two->name . " says " . $animal_two->sound . " give me some " . $animal_two->favorite_food . " my id is " . $animal_two->id . " total animals = " . Dog::$number_of_animals . "<br /><br />";
// 2. Because of method overriding we get different results
$animal_one->run();
$animal_two->run();
// 3. final methods can't be overriden
$animal_one->what_is_good();
// 4. Example using __toString()
echo $animal_two;
// 5. You call a method defined in an interface like all others
$animal_two->sing();
// 6. You can also define functions that will except classes
// extending a secific class or interface
function make_them_sing(Singable $singing_animal)
{
    $singing_animal->sing();
}
// 6. Polymorphism states that different classes can have different
// behaviors for the same function. The compiler is smart enough to