As an object's creation mode, ensure that a class has only one instance, instantiate the class itself, and provide this instance to the system globally.
The code is as follows
<?phpclass single{private static $instance;p rivate function __construct () {}private function __clone () {}public static function getinstance () {if (!self:: $instance instanceof Self) {self:: $instance = new single ();//self:: $instance = New self (); Yes,}return self:: $instance;} Public Function Hello () {echo ' I am a singleton class method produced by Hello ';}} $a = single::getinstance (); $a->hello ();? >
1: private static member variable, saving an instance of a singleton class
2: Private structure, private clone function, ensure only one instance of the Singleton class
3: Common static methods, instantiation of singleton classes
The above three are the necessary elements of a singleton class, and note the way in which the instantiation method is written
Complete!
PHP design mode single-instance mode