Rumah > Artikel > pembangunan bahagian belakang > php实现注册器+工厂+单例模式
<?php//注册器模式class Register{ protetcted static $objects; //声明一个数组 static function set($alias,$objects){ self::objects[$alias]=$objects; } //销毁对象 function _unset($name){ unset($objects[$name]); } staitc function get($name){ return self::$objects[$name]; }}//单例模式class database{ protected static $db; static function getInstance(){ if(self::$db){ //如果对象存在则使用,不存在创建 return self::$db; }else{ return self::$db = new database(); } }}//工厂模式class Factory{static function createDatabase(){ //首先获取到创建的对象 $db = database::getInstance(); $db->set('db1',$db); return $db;}}$db =Factory::createDatabase();