<?php
header('Content-Type:text/html;charset=utf-8');
//单例模式
class Site{
private static $instance=null;//本类的静态实例
private $site_Name='';//私有属性
private function __construct($siteName){//构造方法私有化防止外部实例化
$this->site_Name=$siteName;
}
//获取本类的唯一实例
public static function getInstance($siteName=''){
if(!self::$instance instanceof self){
return self::$instance=new self($siteName);
}
return self::$instance;
}
//克隆方法私有化禁用外部克隆
private function __clone(){
}
}
//工厂模式来生成本类的单一实例
class Factory{
public static function create($siteName=''){
return Site::getInstance($siteName);
}
}
/*对象注册树
* 1,注册 set();把对象挂在树上
* 2,获取 get();把对象从树上取下来
* 3,注销 _unset();把对象吃掉
*/
class Register{
//创建对象池,数组
protected static $objects=[];
//生成对象并上树
public static function set($alias,$objects){
self::$objects[$alias]=$objects;
}
//从树上面取下对象
public static function get($alias){
return self::$objects[$alias];
}
//把树上面的对象吃掉
public static function _unset($alias){
unset(self::$objects[$alias]);
}
}
echo '<pre>';
// Register::set('php中文网','www.php.cn/php2/');
//echo Register::get('php中文网');
//Register::_unset('php中文网');
//将对象放入池里
Register::set('objtree',Factory::create('www.php.cn'));
Register::set('string','PHP程序员工具箱');
//从对象池中去下一个对象
echo '<hr>'; print_r( $obj2=Register::get('string'));
Register::_unset('objtree'); //注销这个对象即把这个对象吃掉
//访问一下这个对象
print_r( $obj=Register::get('objtree'));//注销这个对象后再次访问就会报错
?>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!