mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-09-25 13:59:08 +02:00
start a restructure
This commit is contained in:
58
Creational/Singleton/Singleton.php
Normal file
58
Creational/Singleton/Singleton.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
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)
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
|
||||
if (null === static::$instance) {
|
||||
static::$instance = new static;
|
||||
}
|
||||
|
||||
return static::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* is not allowed to call from outside: private!
|
||||
*
|
||||
*/
|
||||
private function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* prevent the instance from being cloned
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function __clone()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* prevent from being unserialized
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function __wakeup()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user