<?php interface db{ function conn(); } interface Factory{ function createDB(); } class dbmysql implements db{ public function conn(){ echo "连上了mysql数据库"; } } class dbsqlite implements db{ public function conn(){ echo "连上了dbsqlite数据库"; } } class mysqlFactory implements Factory{ public function createDB(){ return new dbmysql(); } } class sqliteFactory implements Factory{ public function createDB(){ return new dbsqlite(); } } //添加数据库oracle class dboracle implements db{ public function conn(){ echo "连接上了oracle数据库"; } } class oracleFactory implements Factory{ public function createDB(){ return new dboracle(); } } //客户端开始 $fact=new mysqlFactory(); $db=$fact->createDB(); $db->conn();