mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-04 05:57:25 +02:00
Merge pull request #35 from olekhy/patch-1
use static class property instead of static variable for Singleton
This commit is contained in:
@@ -19,6 +19,11 @@ namespace DesignPatterns\Singleton;
|
|||||||
*/
|
*/
|
||||||
class Singleton
|
class Singleton
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var cached reference to singleton instance
|
||||||
|
*/
|
||||||
|
protected static $instance;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets the instance via lazy initialization (created on first usage)
|
* gets the instance via lazy initialization (created on first usage)
|
||||||
*
|
*
|
||||||
@@ -26,13 +31,12 @@ class Singleton
|
|||||||
*/
|
*/
|
||||||
public static function getInstance()
|
public static function getInstance()
|
||||||
{
|
{
|
||||||
static $instance;
|
|
||||||
|
if (null === static::$instance) {
|
||||||
if (null === $instance) {
|
static::$instance = new static;
|
||||||
$instance = new self();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $instance;
|
return static::$instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user