note 84804 modified in language.oop5.interfaces by danbrown
| From: | danbrown@php.net | Date: | Wed, 30 Jul 2008 19:03:13 +0000 |
| Subject: | note 84804 modified in language.oop5.interfaces by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142954@lists.php.net to get a copy of this message | ||
While a subclass may implement an interface by extending an abstract class that implements the
interface, I question whether it is good design to to do so. Here's what I would suggest while
taking the liberty of modifying the above weather/wet model:
<?php
interface water
{
public function makeItWet();
}
/**
* abstract class implements water but defines makeItWet
* in the most general way to allow child class to
* provide specificity
**/
abstract class weather implements water
{
private $cloudy;
public function makeItWet(){}
abstract public function start();
abstract public function getCloudy();
abstract public function setCloudy();
}
class rain extends weather {
private $cloudy;
public function start() {
return "Here's some weather. ";
}
public function makeItWet() {
return 'it is raining cats and dogs today.';
}
public function getCloudy() {
return $this->cloudy;
}
public function setCloudy($bln=false) {
$this->cloudy = $bln;
}
}
$a = new rain();
echo $a->start();
$a->setCloudy(true);
if ($a->getCloudy()) {
echo 'It is a cloudy day and ';
}
echo $a->makeItWet();
?>
--was--
While a subclass may implement an interface by extending an abstract class that implements the
interface, I question whether it is good design to to do so. Here's what I would suggest while
taking the liberty of modifying the above weather/wet model:
interface water
{
public function makeItWet();
}
/**
* abstract class implements water but defines makeItWet
* in the most general way to allow child class to
* provide specificity
**/
abstract class weather implements water
{
private $cloudy;
public function makeItWet(){}
abstract public function start();
abstract public function getCloudy();
abstract public function setCloudy();
}
class rain extends weather {
private $cloudy;
public function start() {
return "Here's some weather. ";
}
public function makeItWet() {
return 'it is raining cats and dogs today.';
}
public function getCloudy() {
return $this->cloudy;
}
public function setCloudy($bln=false) {
$this->cloudy = $bln;
}
}
$a = new rain();
echo $a->start();
$a->setCloudy(true);
if ($a->getCloudy()) {
echo 'It is a cloudy day and ';
}
echo $a->makeItWet();
?>
http://php.net/manual/en/language.oop5.interfaces.php