Home >php教程 >php手册 >discuz X的单件模式

discuz X的单件模式

WBOY
WBOYOriginal
2016-06-13 11:26:271283browse

文章介绍了一个关于discuz X的单件模式原创文章,PPC高亮插件太不友好了,我直接贴txt内容都会有奇奇怪怪问题发生。

X的只要文件开头都有这么一句话

 代码如下 复制代码

$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;
}
[/code]
这里保证单词请求都运用一个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]


Statement:
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