PHP Single-instance mode

Source: Internet
Author: User

How is the singleton mode written?

4 steps are generally required to implement a single case

First: A static variable saves the current instance

private static $_instance;

Second: Private constructors prevent external instantiation of objects

Private Function__construct () {}

Third: Private cloning function to prevent the external cloning of objects

Private Function __clone () {}

IV: Static method, single access entry with uniform

public static function getinstance () {

if (Is_null (self::$_instance) | | isset (self::$_instance)) {

Self::$_instance=new self ();

}

return self::$_instance;

}

PHP Single-instance mode

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.