php抽象类实现注册表,用静态调用实现实例,添加注册与显示

<?php
abstract class woo_base_Registry{
	abstract protected function get($key);
	abstract protected function set($key,$val);
}
class woo_base_RequestRegistry extends woo_base_Registry{
	private $values=array();
	private static $instance;
	private function __construct(){}
	static function instance(){
		if(!isset(self::$instance)){
			self::$instance=new self();
		}
		return self::$instance;
	}
	protected function set($key,$val){
		$this->values[$key]=$val;
	}
	protected function get($key){
		if(isset($this->values[$key])){
			return $this->values[$key];
		}
		return null;
	}
	static function getRequest(){
		return self::instance()->get('request');
	}
	static function setRequest(woo_controller_Request $request){
		return self::instance()->set('request',$request);
	}
}
class woo_base_SessionRegistry extends woo_base_Registry{
	private static $instance;
	private function __construct(){
		session_start();
	}
	static function instance(){
		if(!isset(self::$instance)){
			self::$instance=new self();
		}
		return self::$instance;
	}
	protected function get($key){
		if(isset($_SESSION[__CLASS__][$key])){
			return $_SESSION[__CLASS__][$key];
		}
		return null;
	}
	protected function set($key,$val){
		$_SESSION[__CLASS__][$key]=$val;
	}
	function setComplex(Complex $complex){
		self::instance()->set('complex',$complex);
	}
	function getComplex(){
		return self::instance()->get('complex');
	}
}
class woo_base_ApplicationRegistry extends woo_base_Registry{
	private static $instance;
	private $freezedir="data";
	private $values=array();
	private $mtimes=array();
	private function __construct(){}
	static function instance(){
		if(!isset(self::$instance)){
			self::$instance=new self();
		}
		return self::$instance;
	}
	protected function get($key){
		$path=$this->freezedir.DIRECTORY_SEPARATOR.$key;
		if(file_exists($path)){
			clearstatcache();
			$mtime=filemtime($path);
			if(!isset($this->mtimes[$key])){
				$this->mtimes[$key]=0;
			}
			if($mtime > $this->mtimes[$key]){
				$data=file_get_contents($path);
				$this->mtimes[$key]=$mtime;
				return ($this->values[$key]=unserialize($data));
			}
		}
		if(isset($this->values[$key])){
			return $this->values[$key];
		}
		return null;
	}
	protected function set($key,$val){
		$this->values[$key]=$val;
		$path=$this->freezedir.DIRECTORY_SEPARATOR.$key;
		file_put_contents($path,serialize($val));
		$this->mtimes[$key]=time();
	}
	static function getDsn(){
		return self::instance()->get('dsn');
	}
	static function setDsn($dsn){
		return self::instance()->set('dsn',$dsn);
	}
}
class woo_controller_Request{}
class Complex{}
$request=new woo_controller_Request();
$complex=new Complex();
$re=woo_base_RequestRegistry::instance();
woo_base_RequestRegistry::setRequest($request);
print_r(woo_base_RequestRegistry::getRequest('request'));
echo "<br>";
$session=woo_base_SessionRegistry::instance();
 $session->setComplex( $complex );
 print_r($session->getComplex());
$result=woo_base_ApplicationRegistry::instance();
woo_base_ApplicationRegistry::setDsn('bb14524');
echo woo_base_ApplicationRegistry::getDsn();
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值