<?php /* 使用接口 制造厂和实例化直接参数用数组封装,告诉工厂对谁,做什么,实例化的代码就不再做其他事情 */ //各种兵来一个接口 interface bingge { public function make();//制造 public function weixiu();//维修 } //火焰兵 class huoyan implements bingge { public function make() { echo "我是一个火焰兵"; } public function weixiu() { echo "我在维修一个火焰兵"; } } //机枪兵 class jiqiang implements bingge { public function make() { echo "我是一个机枪兵"; } public function weixiu() { echo "我在维修一个机枪兵"; } } //如果第二期需要:坦克兵,就只用在这里添加 class tanke implements bingge { public function make() { echo "我是一个坦克兵!"; } public function weixiu() { echo "我在维修一个坦克兵!"; } } //建造士兵的工具 class makeshibing { public function zuosa($canshu) { $comeon = new $canshu["who"]; $comeon->$canshu["what"](); } } //一个建造实例 $begin = new makeshibing(); $canshu = array ( "who" => "huoyan" , "what" => "make" , ); $how = $begin->zuosa($canshu); echo "<hr>"; //第二期需要增加坦克兵种 $begin = new makeshibing(); $canshu = array ( "who" => "tanke" , "what" => "make" , ); $how = $begin->zuosa($canshu); echo "<hr>"; //第三期需要 $begin = new makeshibing(); $canshu = array ( "who" => "tanke" , "what" => "weixiu" , ); $how = $begin->zuosa($canshu); ?>