An object (before learning the design model, you need to better understand the object-oriented thinking) is only responsible for a specific task. For more information, see
Singleton mode (responsibility mode): Simply put, an object (before learning the design mode, you need to better understand the object-oriented thinking) is only responsible for a specific task. singleton class: 1. the constructor must be marked as private (access control: prevents external code from using the new operator to create objects). a singleton class cannot be instantiated in other classes but can only be instantiated by itself; 2. static member variables of an instance with a storage class. 3. a public static method (commonly used getInstance () method to access this instance) is used to instantiate the singleton class, the instanceof operator can detect whether the class has been instantiated. In addition, you need to create the _ clone () method to prevent the object from being copied (cloned). why is the PHP Singleton mode used? 1. php applications mainly lie in database applications. Therefore, a large number of database operations exist in an application. Using the Singleton mode can avoid the resources consumed by a large number of new operations. 2. if you need a class in the system to globally control some configuration information, you can easily implement it using the Singleton mode. for details, refer to the FrontController section of ZF. 3. in a page request, debugging is easy. because all the code (such as database operation db) is concentrated in a class, we can set hooks in the class and output logs, this avoids var_dump and echo everywhere. Code implementation: <111? 111php111/1 *** Singleton mode of design mode ** $ _ the instance must be declared as a static private variable * the constructor and Destructor must be declared as private, this prevents the new * class of the external program from losing the Singleton mode. * The getInstance () method must be set to public. you must call this method * to return a reference to the instance *:: operators can only access static variables and static functions * new objects consume memory * use cases: database connections are the most common. * After an object is generated in Singleton mode, * this object can be used by many other objects. */Class Danli {// Save the static member variable private static $ _ instance of the class instance; // The constructor of the private tag private function _ construct () {echo 'This is a Constructed method; ';} // create the _ clone method to prevent the object from being copied and cloned. public function _ clone () {trigger_error ('Clone is not allow! ', E_USER_ERROR);} // Singleton method, used to access the public static method of the instance public static function getInstance () {if (! (Self: $ _ instance instanceof self) {self: $ _ instance = new self;} return self: $ _ instance;} public function test () {echo 'method invoked successfully';} // the class that marks the constructor with the new instantiation private will report an error // $ danli = new Danli (); // the correct method, use the double colon: Operator to access the static method to obtain the instance $ danli = Danli: getInstance (); $ danli-> test (); // Copy (clone) the object will cause an E_USER_ERROR $ danli_clone = clone $ danli;