1. The singleton mode reduces resource waste and ensures that there is only one instance object in the entire environment. It is especially suitable for writing resource connection classes.
2. Singleton mode refers to creating only one resource (object, database link, etc.) to prevent external instances from judging whether there is a return or returning an object after creation.
Example
// Singleton mode (mantra: three private and one public) class Singleton{ //Private construction method, prohibit external instantiation of objects private function __construct(){} //Private __clone to prevent the object from being cloned private function __clone(){} //Private internally instantiated objects private static $instance = null; // Public static instance method public static function getInstance(){ if(self::$instance == null){ //Internal instantiation object self::$instance = new self(); } return self::$instance; } }
The above is the function of PHP singleton mode, I hope it will be helpful to everyone. More PHP learning guide: source code network