Home > Backend Development > PHP Tutorial > php singleton mode, php mode_PHP tutorial

php singleton mode, php mode_PHP tutorial

WBOY
Release: 2016-07-13 10:07:25
Original
915 people have browsed it

php singleton mode, php mode

The singleton mode is mainly used for database connection, ensuring that the database has only one instance of a class, and providing this instance to the entire system. This prevents the new operation from consuming resources and avoids too many connection information appearing in the database. There are three main points: 1. There must be only one instance. 2. This instance must be created automatically. 3. This instance must be provided to the entire system. class mysql{ private static $instance ;//Save instance //The constructor is declared as private to prevent direct creation of objects  privete function __construct(){                                                                                      } //Single case method, determine whether it has been instantiated, and only instantiate it once                                  🎜> (){                                                                    self::$instance )){                                                                                                                                            . ();          }                                                                 }   //Prevent cloning objects private function __clone (){                                                                                      }   function test(){           echo "test" ;   } } $conn = mysql::getInstance (); $conn->test (); ?> http://www.bkjia.com/PHPjc/955696.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/955696.htmlTechArticlephp singleton mode, php mode singleton mode is mainly used for database connections to ensure that there is only one instance of a class in the database , and provide this instance to the entire system. So as to avoid new things...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template