Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply content: in actual Singleton mode, to put it bluntly, a class can only be instantiated once. But how can we make an article on this instantiation. In fact, a breakthrough is the magic method of _ construct. This method indicates that this method will be automatically executed when the class is instantiated. What will happen if I change this method into a protected or private one.
Static function getInstance ($ class, $ param = array () {if (! Isset ($ obj [$ class]) {$ obj [$ class] = new $ class ($ param);} return $ obj [$ class];} when instantiating a class, first determine whether there is an instance of this class. if there is one, it will not be instantiated. Otherwise, it will be instantiated.
Amazing! Talk is cheap, show you my code.
The simplest PHP Singleton mode class:
Class TestInstance {public static $ _ instance = null; // The constructor uses protected to prevent external new classes. this is the key to the Singleton mode. protected function _ Construct () {echo 'instance, Instance, Instance .......... ';} // an instance that uses a static variable storage class. it is assigned only when it is instantiated for the first time. in the future, the public static function getInstance () {if (! Isset (self ::$ _ instance) {self ::$ _ instance = new static ();} return self ::$ _ instance ;}}