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 require_once dirname(__FILE__) . '/Dog.php'; $josefine = new Dog("Josefine", "Labrador", "Male"); $josefine->sayWoof(true); $harriet = new Dog("Harriet", "Golden retriever", "Female"); $harriet->sayWoof(false); $harriet->setName("Harry"); $harriet->sayWoof("Male"); $harriet->sayWoof(true);
$this->name = $name; } } class Dog extends Animal { public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } } class Squirrel extends Animal { } class Vet { public function treat(Animal $animal) { echo 'The vet has treated ' . $animal->getName(); } } $cat = new Cat(); $cat->setName('Felix'); $dog = new Dog(); $dog->setName('Ella'); //$s = new Squirrel(); $vet = new Vet(); $vet->treat($cat);
* 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);