In the actual development, the use of a single example mode with a lot of, such as linked database operations, log processing classes and other operations can be used in a single case mode.
Singleton mode is actually a class can only have an instance of a class, then how to achieve this effect? We need to meet 3 conditions when writing a class to achieve a single-mode effect:
The constructor must be declared as a non-public type, so that no other place can produce an instance by new, which also avoids the generation of instances of multiple classes
A static member variable, such as $_instance, that can hold an instance of a class is required in a class.
Having a public static method that accesses this instance, such as getinstance ();
<?phpclass Test {static private $_instance;private function __construct () {}static public Function getinstance () {if (! (Self::$_instance instanceof Self)) {self::$_instance = new self ();} return self::$_instance;}}
This article is from the "Wang Xinhua" blog, make sure to keep this source http://singwa.blog.51cto.com/3086810/1534151