The article introduces an original article about the single mode of discuz
As long as the file of ">
代码如下 | 复制代码 |
$discuz = & discuz_core::instance();
// instance()属于discuz_core类在class_core.php |
// instance() belongs to discuz_core class in class_core.php
function &instance() {
static $object;
if(empty($object)) {
$ object = new discuz_core();
}
return $object;
}
[/code]
This ensures that word requests use a discuz_core instance. The & writing method here is for compatibility with PHP4. If it is in PHP5, you can use static.
[code language=php]
//Here is a simple example of the singleton pattern.
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 'same object';
} else {
echo 'Not the same object';
}
[/code]