小例子:
需求:公司定义一个接口让我们开发功能
usb.interface.php:
<?php
interface USB{ public function run();
}
store.class.php:
<?php
include_once("./usb.interface.php");
class store implements USB{ public function run(){
$this -> initialize();
} private function initialize(){
echo "store running ..";
}
}
mouse.class.php:
<?php
include_once("./usb.interface.php");
class mouse implements USB{ public function run(){
$this -> init();
} public function init(){
echo "mouse running ...";
}
}
key.class.php:
<?php
include_once("./usb.interface.php");
class key implements USB{ public function run(){
$this -> init();
} public function init(){
echo "key running ..";
}
}
使用:computer.class.php
<?php
include("./mouse.class.php");
include("./store.class.php");
include("./key.class.php"); class computer{ public function useUSB($obj){
$obj -> run();
}
} $computer = new computer(); $computer -> useUSB(new mouse());
echo "<hr />";
$computer -> useUSB(new store());
echo "<hr />";
$computer -> useUSB(new key());