mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
Update Singleton.php
Hi! I'm sure current realization of singleton is classic variance of the pattern. But not really correct (more then is mistaken for me) in relation to real codding on PHP projects. ;) ```php class A extends Singleton // Singleton which used static inside of getInstance() method {} class B extends A {} $a = A::getInstance(); $b = B::getInstance(); $c = Singleton::getInstance(); $a === $b && $b === $c; // returned FALSE ``` when u would be utilized Singleton updated then expression above returned TRUE.
This commit is contained in:
@@ -16,6 +16,11 @@ namespace DesignPatterns;
|
||||
*/
|
||||
class Singleton
|
||||
{
|
||||
/**
|
||||
* @var cached reference to singleton instance
|
||||
*/
|
||||
protected static $instance;
|
||||
|
||||
/**
|
||||
* gets the instance via lazy initialization (created on first usage)
|
||||
*
|
||||
@@ -23,13 +28,12 @@ class Singleton
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
static $instance;
|
||||
|
||||
if (null === $instance) {
|
||||
$instance = new self();
|
||||
|
||||
if (null === static::$instance) {
|
||||
static::$instance = new static;
|
||||
}
|
||||
|
||||
return $instance;
|
||||
return static::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user