Home >Backend Development >PHP Tutorial >PHP singleton mode usage scenarios and usage methods
A class has only one object instance
1. Meaning
As an object creation mode, the singleton mode ensures that a certain class has only one instance. And instantiate it yourself and provide this instance globally to the entire system. It does not create a copy of the instance, but returns a reference to the instance stored inside the singleton class.
Related learning recommendations: PHP programming from entry to proficiency
2. Three key points of singleton mode:
(1). A static member variable is needed to save the only instance of the class:
private static $_instance;
(2). The constructor and clone function must be declared private to prevent external programs from creating new classes and thus losing the meaning of the singleton mode. :
private function __construct(){ $this->_db = pg_connect('xxxx'); } private function __clone(){ }//覆盖__clone()方法,禁止克隆
(3). You must provide a public static method to access this instance (usually the getInstance method), thereby returning a reference to the only instance
public static function getInstance(){ if(! (self::$_instance instanceof self) ){ self::$_instance = new self(); } return self::$_instance;
Single case mode Ensure that a class has only one instance, instantiates itself and provides this instance to the entire system.
The singleton mode is a common design pattern. In computer systems, thread pools, caches, log objects, dialog boxes, printers, database operations, and graphics card drivers are often designed as singletons.
There are three types of singleton modes: lazy-style singleton, hungry-style singleton, and registration-style singleton.
The singleton mode has the following three characteristics:
1. There can only be one instance.
2. You must create this instance yourself.
3. This instance must be provided to other objects.
So why use PHP singleton mode?
One of the main application scenarios of PHP is the scenario where the application deals with the database. In an application, there will be a large number of database operations. For the behavior of connecting the database handle to the database, using the singleton mode can avoid a large number of operations. new operation. Because every new operation consumes system and memory resources.
class Single { private $name;//声明一个私有的实例变量 private function __construct(){//声明私有构造方法为了防止外部代码使用new来创建对象。 } static public $instance;//声明一个静态变量(保存在类中唯一的一个实例) static public function getinstance(){//声明一个getinstance()静态方法,用于检测是否有实例对象 if(!self::$instance) self::$instance = new self(); return self::$instance; } public function setname($n){ $this->name = $n; } public function getname(){ return $this->name; } } $oa = Single::getinstance(); $ob = Single::getinstance(); $oa->setname('hello world'); $ob->setname('good morning'); echo $oa->getname();//good morning echo $ob->getname();//good morning
The above is the detailed content of PHP singleton mode usage scenarios and usage methods. For more information, please follow other related articles on the PHP Chinese website!