Example #1
0
    public function __construct($name)
    {
        $this->name = $name;
        $this->hp = 150;
    }
    public function pet()
    {
        $this->hp += 5;
    }
}
//---------END OF DOG CLASS CODE-------------//
$dog1 = new Dog('bill');
$dog1->walk();
$dog1->walk();
$dog1->walk();
$dog1->run();
$dog1->run();
$dog1->pet();
$dog1->display_hp();
//-----DRAGON CLASS------------------------//
class Dragon extends Animal
{
    public function __construct($name)
    {
        $this->name = $name;
        $this->hp = 170;
    }
    public function fly()
    {
        $this->hp -= 10;
    }
Example #2
0
    }
    public function __get($key)
    {
        return $this->data[$key];
    }
    public function __call($name, $arguments)
    {
        var_dump($name);
        var_dump($arguments);
    }
    public static function __callStatic($name, $arguments)
    {
        var_dump($name);
        var_dump($arguments);
    }
    public function bark()
    {
        echo "Bark!";
    }
    public static function warning()
    {
        echo "Dog Bite!";
    }
}
Dog::run("Pyay", 300);
$dog = new Dog("Aung Net", "Red");
$dog->test = "Testing";
$dog->goo = "Foo";
echo $dog->test;
echo $dog->goo;
$dog->dance("Aung Net", 4);
// 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
// just figure out which function to execute