As a common design mode, Singleton mode is widely used. So how to design a singleton is the best? We usually write this way, and most examples that can be found on the Internet are as follows: classA {protectedstatic $ _ instancenull; protectedfunction _ construct () {disallownewinstance} pr
As a common design mode, Singleton mode is widely used. So how to design a singleton is the best? This is usually the case. Most examples found on the Internet are as follows: class A {protected static $ _ instance = null; protected function _ construct () {// disallow new instance} pr
As a common design mode, Singleton mode is widely used. So how to design a singleton is the best?
We usually write this way, and most examples that can be found on the Internet are as follows:
class A{ protected static $_instance = null; protected function __construct() { //disallow new instance } protected function __clone(){ //disallow clone } public function getInstance() { if (self::$_instance === null) { self::$_instance = new self(); } return self::$_instance; }}class B extends A{ protected static $_instance = null;}$a = A::getInstance();$b = B::getInstance();var_dump($a === $b);
Set the _ construct Method to private to prevent this class from being instantiated by others. But the obvious problem with this writing method is that the Code cannot be reused. For example, A class inherits:
class B extends A{ protected static $_instance = null;}$a = A::getInstance();$b = B::getInstance();var_dump($a === $b);
The above code will output:
bool(true)
The problem lies in self. the reference of self is determined when the class is defined. That is to say, if A inherits B, its reference of self still points to. To solve this problem, the static binding feature is introduced in PHP 5.3. In short, the static keyword is used to access static methods or variables. Unlike self, static references are determined by the runtime. So we simply rewrite our code so that the singleton mode can be reused.
class C{ protected static $_instance = null; protected function __construct() { } protected function __clone() { //disallow clone } public function getInstance() { if (static::$_instance === null) { static::$_instance = new self(); } return static::$_instance; } }class D extends C{ protected static $_instance = null;}$c = C::getInstance();$d = D::getInstance();var_dump($c === $d);
The above code output:
bool(false)
In this way, the singleton mode can be implemented simply by inheriting and re-initializing the $ _ instance variable. Note that the above method can only be used in PHP 5.3. For PHP of the previous version, write a getInstance () method for each Singleton class.
It should be noted that although the single-sample mode in PHP does not have the same thread security issues as in Java, you must be careful when using the single-instance mode for stateful classes. Classes in the singleton mode will be accompanied by the entire lifecycle of PHP running, which is also an overhead for memory.
Original article address: PHP Singleton mode best practices. Thank you for sharing it with me.