PHP Development Model-Singleton mode

Source: Internet
Author: User

Class Db{private function __construct () {}private static $instance;p ublic static function getinstance () {if (!self::$ Instance instanceof Db) {self:: $instance = new self ();} Return self:: $instance;} Private Function __clone () {}} $db 1= db::getinstance (); $db 2= db::getinstance (); $db 3= db::getinstance (); Var_dump ($db 1) ; Var_dump ($db 2); Var_dump ($db 3);

The singleton mode, also known as the duty mode, is used to create a single-function access point in the program, which is, in layman's name, unique to the instantiated object.
all singleton modes have at least the following three common elements:
1. They must have a constructor and must be marked as private
2. They have a static member variable that holds an instance of the class
3. They have a public static method that accesses this instance
a singleton class cannot be instantiated directly in any other class, but only by its own instantiation. Instead of creating an instance copy, it returns a reference to the instance stored inside the singleton class.

PHP Development Model-Singleton 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.