Skip to content

Commit

Permalink
Added SingletonException
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Jun 4, 2018
1 parent e9a897a commit 2fcc59b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ nbproject/private
*~
composer.lock
vendor
.idea
7 changes: 0 additions & 7 deletions nbproject/project.properties

This file was deleted.

9 changes: 0 additions & 9 deletions nbproject/project.xml

This file was deleted.

41 changes: 21 additions & 20 deletions src/Singleton.php
Expand Up @@ -2,32 +2,33 @@

namespace ByJG\DesignPattern;

use Exception;

trait Singleton
{
protected function __construct()
{ }
protected function __construct()
{
}

/**
* @throws SingletonException
*/
final private function __clone()
{
throw new SingletonException('You can not clone a singleton.');
}

final private function __clone()
{
throw new Exception('You can not clone a singleton.');
}

/**
* @return static
*/
public static function getInstance()
{
static $instances;
/**
* @return static
*/
public static function getInstance()
{
static $instances;

$calledClass = get_called_class();

if (!isset($instances[$calledClass]))
{
if (!isset($instances[$calledClass])) {
$instances[$calledClass] = new $calledClass();
}
return $instances[$calledClass];
}
}
return $instances[$calledClass];
}

}
8 changes: 8 additions & 0 deletions src/SingletonException.php
@@ -0,0 +1,8 @@
<?php

namespace ByJG\DesignPattern;

class SingletonException extends \Exception
{

}

0 comments on commit 2fcc59b

Please sign in to comment.