Daily PHP Single-instance mode implementation

Source: Internet
Author: User
Daily PHP Single-instance mode implementation

The singleton mode, as the name implies, only requires only one instanced object, in the software design, the singleton mode is very common. Instantiated once, save a lot of resources, and the singleton mode to some extent can replace the introduction of global variables in the class caused by the bug. Because global variables bundle classes into a particular environment. The encapsulation is broken, and such classes are difficult to be universally used by other applications. Using singleton mode allows classes to be better organized and reused.

Let's take a look at the implementation of the singleton pattern in PHP

 
 



Calling code

$singleton = Singleton::getinstance (), $singleton 2 = singleton::getinstance (); Var_dump ($singleton = = = $singleton 2);



In the call code above, it will print true at the end, so that we can simply implement the singleton pattern.




  • 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.