interface db { public function conn(); } class dbmysql implements db{ public function conn() { echo ‘mysql is conn‘; } } class dbsqlite implements db{ public function conn() { echo ‘sqlite is conn‘; } } //简单工厂模式 class gongchang{ public static function createDB($type){ if($type==‘mysql‘){ return new dbmysql(); }else{ return new dbsqlite(); } } } $db = gongchang::createDB(‘mysql‘); $db->conn();
时间: 2024-10-24 21:11:10