illustrate
1. The singleton pattern is very useful when you need to ensure that there is only one instance of an object. By giving control of object creation to a single point, only one instance of the application exists at any time. Singletons should not be instantiated outside a class.
Notice
2. Access to the private constructor is required to prevent the class from being instantiated randomly.
Static variables must hold class instances.
There must be public static methods accessing this instance. This method is usually named getInstance()
There must be a private, empty clone method to prevent copying.
Example
class Single { public static $_instance; private function __construct() { } private function __clone() { } public static function getInstance() { if (!self::$_instance) { self::$_instance = new self(); } return self::$_instance; } public function sayHi() { echo "Hi n"; } } $single = Single::getInstance(); $single->sayHi();
The above is the understanding of PHP singleton mode, I hope it will be helpful to everyone.