Talk about the Singleton_PHP tutorial

Source: Internet
Author: User
Let's talk about Singleton in the PHP Singleton mode. The Singleton mode is a creation mode. it is the simplest mode in the design mode. of course, it is used everywhere. The Singleton mode ensures that one class has only one instance, and provides the Singleton mode as the creation mode. it is the simplest mode in the design mode, and of course it is used everywhere.

The Singleton mode ensures that a class has only one instance and provides a global access point to access it.

When you need to control the number of instances of a class and the caller can access it from a public, well-known access point, we can consider using the Singleton mode.

We use UML to design the Singleton mode. of course, we will use UML to describe our design in the design part of the future design mode, which is more visualized.

From the UML Design diagram, we can see that in order for a class to have only one Instance, it must create a static variable, and then we use a public static Instance () method to create it, however, to avoid the class's own constructor being able to create objects, we set the constructor to protected or private, so that the external can only use Instance () to create a static Singleton class. It seems that we have achieved our goal. next we will look at the code:

public class Singleton  {    private static Singleton instance;    protected Singleton()    public static Singleton Instance() {        if(instance != null) instance = new Singleton();        return instance;    }}

From this point of view, we can do the following steps to implement the Singleton mode:

  1. Create a static variable in the class. the variable type is the current class;
  2. Create a public static method in the class so that you can use this method to create such static objects;
  3. Finally, set the constructor to protected or private.
Program List: the simplest singleton class
 
Program List: Scalable singleton class

An Extensible singleton class seems impossible, but the following program is very close to this effect.

    
 
 
 rndId = rand();    }            public function whatAmI(){        echo 'I am a Apple('.$this->rndId.')
'; } public static function getInstance(){ return Fruit::getSingleton(get_class()); }}class GreenApple extends Apple { public function whatAmI(){ echo 'I am a GreenApple('.$this->rndId.')
'; } public static function getInstance(){ return Fruit::getSingleton(get_class()); }}$apple1 = Apple::getInstance();$apple2 = GreenApple::getInstance();$apple1->whatAmI();// should echo 'I am a A(some number)$apple2->whatAmI();// should echo 'I am a B(some number)$apple1 = Apple::getInstance();$apple2 = GreenApple::getInstance();$apple1->whatAmI();// should echo 'I am a A(same number as above)$apple2->whatAmI();// should echo 'I am a B(same number as above)// $a = new A();// this should fail// $b = new B();// this should fail?>

Program running result:

I am a Apple(4462)I am a GreenApple(8207)I am a Apple(4462)I am a GreenApple(8207)
Program List: singleton class and its derived class
  
 showColor();?>

Program running result:

I am constructedMy Color is Red.

Bytes. The Singleton mode ensures that one class has only one instance and provides...

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.