<?php
/**
* 单例模式
*/
class Site
{
public $siteName;
//静态实例
protected static $instance=null;
//禁用构造器
private function __construct($siteName)
{
$this->siteName=$siteName;
}
//获取本例唯一实例
public static function getInstance($siteName='微语录')
{
if(!self::$instance instanceof self){
self::$instance = new self($siteName);
}
return self::$instance;
}
}
/**
* 用工厂模式生成单一实例
*/
class Factory
{
public static function create($iteam='www.top789.cn')
{
return Site::getInstance($iteam);
}
}
/**
* 对象注册树
* 1. 注册set()把对象挂上树
* 2. 获取get()把对象取下来
* 3. 注销_unset()把对象吃掉
*/
class Register
{
protected static $objects=[];
public static function set($key,$object)
{
self::$objects[$key]=$object;
}
public static function get($key)
{
return self::$objects[$key];
}
public static function _unset($key)
{
unset(self::$objects[$key]);
}
}
//使用
Register::set('sitetemp',Factory::create('测试'));
$obj=Register::get('sitetemp');
var_dump($obj);
echo $obj->siteName;