Simple way to share PHP with a single-state design pattern

Source: Internet
Author: User
This article mainly introduces PHP simple implementation of the single-state design mode, simple analysis of the configuration of the single-state design mode, implementation and use of methods, the need for friends can refer to the next

In this paper, we describe the simple method of implementing single-state design mode in PHP. Share to everyone for your reference, as follows:

The single-state design pattern typically consists of the following three points:

· A private construction method; (Ensure that the user cannot instantiate it by creating an object)
· A public static method; (responsible for instantiating itself)
· a private static property; (for saving only one instantiated object)


<?phpclass singleton{    //For saving only one instantiated object    private static $Instance =null;    The constructor method uses the private wrapper to create the object    Private function __construct () {}    only within the class using new. This method is the only way to return an object in this class, which is a static method that calls public    static getinstance () {       if (self:: $Instance instanceof-Self) with the class name {// If $instance in this class is empty, the description has not been instantiated too self         :: $Instance =new SingleTon ();//Instantiate this object       } return self       :: $Instance;    }} $instance =singleton::getinstance (); You can only use the static method getinstance () to get the object of the Singleton class?>

The so-called single-state design pattern is that a class can only produce/create a unique object

to write a singleton design pattern, you must have a class instantiate only one object, and to make a class only instantiate an object, first let a class not instantiate an object

Related Article

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.