PHP Interface and Abstract Class example

abstract class animal
{
abstract function getowned();
private $age;

protected function __construct($age) {
$this->age = $age;
}

public function getage()
{
return $this->age;
}
}
interface insurable {
public function getvalue();
}

class pet extends animal implements insurable {
private $name;
public function __construct($name,$age) {
parent::__construct($age);
$this->name = $name;
}
public function getname() {
return $this->name;
}
public function getowned() {
return (“Owner String”);
}
public function getvalue() {
return (“Priceless”);
}
}

class house implements insurable {
public function getvalue() {
return (“Rising fast”);
}

}

$charlie = new pet(“Charlie”,6);
$catage = $charlie -> getage();
$catname = $charlie -> getname();
print “$catname is $catage years old!<br><br>”;

if ($charlie instanceof pet) print (“charlie is a pet<br>”);
if ($charlie instanceof animal) print (“charlie is an animal<br>”);
if ($charlie instanceof house) print (“charlie is a house<br>”);

if ($charlie instanceof insurable) print (“charlie is insurable<br>”);
reference: http://www.wellho.net/resources/ex.php4?item=h108/animal3.php

Comments are closed.