PHP设计模式 注册表模式

yipeiwu_com6年前PHP代码库
下面是基本的注册表类的代码:
复制代码 代码如下:

<?php
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
?>

注册表的作用是提供系统级别的对象访问功能。

相关文章

在任意字符集下正常显示网页的方法二(续)

转:coolcode.cn 前几天写了一篇在任意字符集下正常显示网页的方法,里面介绍的很简单,就是把前128个字符以外的字符集都用 NCR 来表示,但是具体怎么转化我...

PHP实现在对象之外访问其私有属性private及保护属性protected的方法

本文实例讲述了PHP实现在对象之外访问其私有属性private及保护属性protected的方法。分享给大家供大家参考,具体如下: public 表示全局的访问权限,类内部外部子类都可以...

PHP中去掉字符串首尾空格的方法

第一种方法:通过php自带的函数 <?php /* trim 去除一个字符串两端空格, rtrim 是去除一个字符串右部空格, ltrim 是去除一个字符串左部空格。 */ ?&g...

php 按指定元素值去除数组元素的实现方法

按指定元素值去除数组元素 复制代码 代码如下: <?php //去除值为"Cat"的元素 $a=array("a"=>"Dog","b"=>"Cat","c"=>...

用PHP程序实现支持页面后退的两种方法

  第一,使用Header方法设置消息头Cache-control QUOTE: header('Cache-control: private, ...