<?php
abstract class Registry{
abstract protected function set($key,$value);
abstract protected function get($key);
}
class RequestRegistry extends Registry{
private static $instance;
private $values=array();
static function instance(){
if(!isset(self::$instance)){
self::$instance=new self();
}
return self::$instance;
}
protected function set($key,$value){
$this->values[$key]=$value;
}
protected function get($key){
return $this->values[$key];
}
function getAaa(){
return self::instance()->get('Aaa');
}
function setAaa($aaa){
self::instance()->set("Aaa",$aaa);
}
}
class SessionRegistry extends Registry{
private static $instance;
private $values=array();
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){
return $this->values[$key];
}
function getBbb($key){
return self::instance()->get($key);
}
function setBbb($key,$val){
self::instance()->set($key,$val);
}
}
class reg{}
$re=SessionRegistry::instance();
$re->setBbb('Bbb',new reg());
print_r($re->getBbb('Bbb'));
$ss=RequestRegistry::instance();
$ss->setAaa('aaa');
echo $ss->getAaa('Aaa');
?>
php注册表,利用抽象基类,在子类实现功能,添加和获取,即可以是字符也可以是对象
最新推荐文章于 2024-08-13 15:52:19 发布
本文介绍了如何使用PHP的abstract class Registry创建Registry抽象类,并展示了RequestRegistry和SessionRegistry子类的应用,通过实例化和操作共享数据。
摘要由CSDN通过智能技术生成