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:
Oleksandr
2013-09-21 22:05:13 +02:00
parent 65910a95a2
commit 52ef48fd28

View File

@@ -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;
}
/**