<?php
//飞行接口
interface Flyable {
function fly($obj);
}
//飞行接口类的具体实现
class Action implements Flyable{
function fly($obj){
if($obj instanceof Aduck){
echo ‘Aduck is flying<br/>‘;
}else if($obj instanceof Bduck){
echo ‘Bduck is flying<br/>‘;
}else{
echo ‘cannot fly<br/>‘;
}
}
}
//基类
class Duck {
protected $_flyAction;
public function __construct(){
$this->_flyAction = new Action();
}
public function shape(){
echo ‘duck shape‘;
}
public function life() {
echo ‘a duck life‘;
}
}
//A子类
class Aduck extends Duck {
public function fly(){
echo $this->_flyAction->fly($this);
}
}
//B子类
class Bduck extends Duck {
public function fly(){
echo $this->_flyAction->fly($this);
}
}
//C子类
class Cduck extends Duck {
public function fly(){
echo $this->_flyAction->fly($this);
}
}
$a = new Aduck();
$b = new Bduck();
$c = new Cduck();
$a->fly();
$b->fly();
$c->fly();