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