PHP design mode single-instance mode

Source: Internet
Author: User

As an object's creation mode, ensure that a class has only one instance, instantiate the class itself, and provide this instance to the system globally.

The code is as follows

<?phpclass single{private static $instance;p rivate function __construct () {}private function __clone () {}public static function getinstance () {if (!self:: $instance instanceof Self) {self:: $instance = new single ();//self:: $instance = New self (); Yes,}return self:: $instance;} Public Function Hello () {echo ' I am a singleton class method produced by Hello ';}} $a = single::getinstance (); $a->hello ();? >

1: private static member variable, saving an instance of a singleton class

2: Private structure, private clone function, ensure only one instance of the Singleton class

3: Common static methods, instantiation of singleton classes

The above three are the necessary elements of a singleton class, and note the way in which the instantiation method is written

Complete!

PHP design mode 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.