Discuz X's single-piece mode

Source: Internet
Author: User

X, as long as there's a word at the beginning of the file.

The code is as follows Copy Code

$discuz = & Discuz_core::instance ();
[code language=php]

 

The

//instance () belongs to the Discuz_core class in the class_core.php
function &instance () {
static $object;
If Empty ($object ) {
  $object = new Discuz_core ();
}
return $object;
}
[/code]
guarantees that word requests use a Discuz_core instance. The & is written here to be compatible with PHP4, and if you are in PHP5, you can use static.
[code language=php]
///This is a simple example of a single piece pattern.
Class Phpig {
private static $v = null;
static function instance () {
  if (self:: $v = null) {
&N bsp;  self:: $v = new Phpig ();
 }
  return self:: $v;
}

$pig 1 = phpig::instance ();
$pig 2 = phpig::instance ();
if ($pig 1 = = $pig 2) {
echo ' same object ';
else {
echo ' is not the same object ';
}
[/code]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.