public function testDog()
 {
     $dog = new Dog("Benji");
     $this->assertEquals("Benji", $dog->getName());
     $this->assertEquals("Benji the Dog", (string) $dog);
     $dog->setName("Rover");
     $this->assertEquals("Rover", $dog->getName());
     $this->assertEquals("Rover the Dog", (string) $dog);
 }
<?php

include "Dog.php";
include "Bird.php";
$animal = new Animal("jeffery the lion");
$animal->getName();
$doggy = new Dog("Timmy");
$doggy->getName();
$doggy->move();
$doggy->digHole();
$doggy->tryToFly();
$birdy = new Bird("Tweety");
$birdy->getName();
$birdy->move();
$birdy->tryToFly();
示例#3
0
 * Created by PhpStorm.
 * User: Jundat95
 * Date: 12/24/2015
 * Time: 9:24 AM
 */
abstract class Animal
{
    protected $name;
    public abstract function setName($name);
    public function getName()
    {
        return $this->name;
    }
}
class Dog extends Animal
{
    public $age = 5;
    public function getAge()
    {
        return $this->age;
    }
    // Override lai function Abstract cua class Animal
    public function setName($name)
    {
        $this->name = $name;
    }
}
$dog = new Dog();
$dog->setName('Dog Family');
echo $dog->getName();
var_dump($dog);
示例#4
0
<?php

class Animal
{
    private $name;
    public function __construct($name)
    {
        $this->name = $name;
    }
    final function getName()
    {
        echo $this->name;
    }
}
class Dog extends Animal
{
    public function __construct($name)
    {
        echo 'Yo dawg!' . '<br />' . PHP_EOL;
        parent::__construct($name);
    }
}
$dog = new Dog('Sparky');
echo $dog->getName() . '<br />' . PHP_EOL;