1
1 <?php 2 //创建单例 3 class Single{ 4 public $hash; 5 static protected $ins=null; 6 final protected function __construct(){ 7 $this->hash=rand(1,9999); 8 } 9 10 static public function getInstance(){ 11 if (self::$ins instanceof self) { 12 return self::$ins; 13 } 14 self::$ins=new self(); 15 return self::$ins; 16 } 17 } 18 19 //工厂模式 20 class RandFactory{ 21 public static function factory(){ 22 return Single::getInstance(); 23 } 24 } 25 26 //注册树 27 class Register{ 28 protected static $objects; 29 public static function set($alias,$object){ 30 self::$objects[$alias]=$object; 31 } 32 public static function get($alias){ 33 return self::$objects[$alias]; 34 } 35 public static function _unset($alias){ 36 unset(self::$objects[$alias]); 37 } 38 } 39 40 Register::set('rand',RandFactory::factory()); 41 42 $object=Register::get('rand'); 43 44 print_r($object);