mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-09 00:16:32 +02:00
PHP7 Singleton
This commit is contained in:
@@ -2,22 +2,17 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Creational\Singleton;
|
namespace DesignPatterns\Creational\Singleton;
|
||||||
|
|
||||||
/**
|
|
||||||
* class Singleton.
|
|
||||||
*/
|
|
||||||
class Singleton
|
class Singleton
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Singleton reference to singleton instance
|
* @var Singleton
|
||||||
*/
|
*/
|
||||||
private static $instance;
|
private static $instance;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets the instance via lazy initialization (created on first usage).
|
* gets the instance via lazy initialization (created on first usage)
|
||||||
*
|
|
||||||
* @return self
|
|
||||||
*/
|
*/
|
||||||
public static function getInstance()
|
public static function getInstance(): Singleton
|
||||||
{
|
{
|
||||||
if (null === static::$instance) {
|
if (null === static::$instance) {
|
||||||
static::$instance = new static();
|
static::$instance = new static();
|
||||||
@@ -27,33 +22,30 @@ class Singleton
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* is not allowed to call from outside: private!
|
* is not allowed to call from outside to prevent from creating multiple instances,
|
||||||
|
* to use the singleton, you have to obtain the instance from Singleton::getInstance() instead
|
||||||
*/
|
*/
|
||||||
private function __construct()
|
private function __construct()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* prevent the instance from being cloned.
|
* prevent the instance from being cloned (which would create a second instance of it)
|
||||||
*
|
*
|
||||||
* @throws SingletonPatternViolationException
|
* @throws SingletonPatternViolationException
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
final public function __clone()
|
final public function __clone()
|
||||||
{
|
{
|
||||||
throw new SingletonPatternViolationException('This is a Singleton. Clone is forbidden');
|
throw new \Exception('This is a Singleton. __clone is forbidden');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* prevent from being unserialized.
|
* prevent from being unserialized (which would create a second instance of it)
|
||||||
*
|
*
|
||||||
* @throws SingletonPatternViolationException
|
* @throws SingletonPatternViolationException
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
final public function __wakeup()
|
final public function __wakeup()
|
||||||
{
|
{
|
||||||
throw new SingletonPatternViolationException('This is a Singleton. __wakeup usage is forbidden');
|
throw new \Exception('This is a Singleton. __wakeup is forbidden');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,32 +4,19 @@ namespace DesignPatterns\Creational\Singleton\Tests;
|
|||||||
|
|
||||||
use DesignPatterns\Creational\Singleton\Singleton;
|
use DesignPatterns\Creational\Singleton\Singleton;
|
||||||
|
|
||||||
/**
|
|
||||||
* SingletonTest tests the singleton pattern.
|
|
||||||
*/
|
|
||||||
class SingletonTest extends \PHPUnit_Framework_TestCase
|
class SingletonTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function testUniqueness()
|
public function testUniqueness()
|
||||||
{
|
{
|
||||||
$firstCall = Singleton::getInstance();
|
$firstCall = Singleton::getInstance();
|
||||||
$this->assertInstanceOf('DesignPatterns\Creational\Singleton\Singleton', $firstCall);
|
|
||||||
$secondCall = Singleton::getInstance();
|
$secondCall = Singleton::getInstance();
|
||||||
|
|
||||||
|
$this->assertInstanceOf('DesignPatterns\Creational\Singleton\Singleton', $firstCall);
|
||||||
$this->assertSame($firstCall, $secondCall);
|
$this->assertSame($firstCall, $secondCall);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNoConstructor()
|
|
||||||
{
|
|
||||||
$obj = Singleton::getInstance();
|
|
||||||
|
|
||||||
$refl = new \ReflectionObject($obj);
|
|
||||||
$meth = $refl->getMethod('__construct');
|
|
||||||
$this->assertTrue($meth->isPrivate());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \DesignPatterns\Creational\Singleton\SingletonPatternViolationException
|
* @expectedException \DesignPatterns\Creational\Singleton\SingletonPatternViolationException
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function testNoCloneAllowed()
|
public function testNoCloneAllowed()
|
||||||
{
|
{
|
||||||
@@ -39,13 +26,11 @@ class SingletonTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \DesignPatterns\Creational\Singleton\SingletonPatternViolationException
|
* @expectedException \DesignPatterns\Creational\Singleton\SingletonPatternViolationException
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function testNoSerializationAllowed()
|
public function testNoSerializationAllowed()
|
||||||
{
|
{
|
||||||
$obj1 = Singleton::getInstance();
|
$obj1 = Singleton::getInstance();
|
||||||
$serialized = serialize($obj1);
|
$serialized = serialize($obj1);
|
||||||
$obj2 = unserialize($serialized);
|
unserialize($serialized);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user