Home > php教程 > php手册 > PHP设计模式 注册表模式

PHP设计模式 注册表模式

WBOY
Release: 2016-06-13 12:02:15
Original
1309 people have browsed it

下面是基本的注册表类的代码:

复制代码 代码如下:


class Registry {
private static $instance;
private $request;//注册表的内容类
private function __construct(){}//本类,不可实例化
static function instance(){//单例类,通过这个方法返回实例
if (!isset(self::$instance)){self::$instance=new self();}
return self::$instance;
}
function getRequest(){//返回注册的内容类
return $this->request;
}
function setRequest(request $request){//设置注册的内容类
$this->request=$request;
}
}
class request{//被 注册的类
private $webname="WEB开发笔记";
private $url="www.chhua.com";
function getName(){
echo $this->url;//输出www.chhua.com
}
}//被注册的空类
//使用
$reg=Registry::instance();
$reg->setRequest(new request());
$request=$reg->getRequest();
$request->getName();//输出www.chhua.com
?>


注册表的作用是提供系统级别的对象访问功能。
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template