Single piece mode of discuz X_PHP tutorial

WBOY
Release: 2016-07-20 11:10:38
Original
1065 people have browsed it

The article introduces an original article about the single mode of discuz

As long as the file of ">

The code is as follows Copy 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;
}
[/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]

$discuz = & discuz_core::instance();
[code language=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]

http://www.bkjia.com/PHPjc/444705.html
www.bkjia.comtrue
http: //www.bkjia.com/PHPjc/444705.htmlTechArticleThe article introduces an original article about the single mode of discuz X. The PPC highlighting plug-in is too unfriendly. I If you post txt content directly, strange problems will occur. As long as X is present at the beginning of the file...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!