discuz X的单件模式
2013-09-05 19:26
148 查看
文章介绍了一个关于discuz X的单件模式原创文章,PPC高亮插件太不友好了,我直接贴txt内容都会有奇奇怪怪问题发生。X的只要文件开头都有这么一句话
这里保证单词请求都运用一个discuz_core实例。这里的&写法是为了兼容PHP4的,如果在PHP5中,则可以使用static。
[code language=php]
//这里是单件模式的简单例子。
class PHPig {
private static $v = null;
static function instance() {
if(self::$v == null) {
self::$v = new PHPig();
}
return self::$v;
}
}
$pig1 = PHPig::instance();
$pig2 = PHPig::instance();
if($pig1 === $pig2) {
echo '同一个对象';
} else {
echo '不是同一个对象';
}
[/code]
[/code]
$discuz = & discuz_core::instance(); [code language=php] // instance()属于discuz_core类在class_core.php function &instance() { static $object; if(empty($object)) { $object = new discuz_core(); } return $object; }
这里保证单词请求都运用一个discuz_core实例。这里的&写法是为了兼容PHP4的,如果在PHP5中,则可以使用static。
[code language=php]
//这里是单件模式的简单例子。
class PHPig {
private static $v = null;
static function instance() {
if(self::$v == null) {
self::$v = new PHPig();
}
return self::$v;
}
}
$pig1 = PHPig::instance();
$pig2 = PHPig::instance();
if($pig1 === $pig2) {
echo '同一个对象';
} else {
echo '不是同一个对象';
}
[/code]
[/code]
相关文章推荐
- 【剑指offer】题2:实现单件(singleton)模式
- 单件模式
- .NET设计模式(2):单件模式(Singleton Pattern)
- [设计模式] C++设计模式-单件 Good!!!
- 单件模式
- 设计模式:单件模式
- discuz7.2 firefox下编辑器模式选择不显示
- 设计模式笔记1:单件模式
- 3.5单件模式(singleton)
- 设计模式 单件模式
- 多线程Singleton单件模式
- HeadFirst设计模式学习笔记3-单件模式
- 设计模式六:singleton单件模式——对象创建型模式
- 闲话单件模式
- PHP设计模式之单件(Singleton)
- 用C#语言泛化单件模式
- 单件模式(Singleton Pattern)
- C++设计模式六--SingletonPattern(单件模式)
- 单件模式(Singleton Pattern)(转自TerryLee)
- [iOS] Objective-C的单件模式