Skip to content

Commit 2fcc59b

Browse files
committed
Added SingletonException
1 parent e9a897a commit 2fcc59b

File tree

5 files changed

+30
-36
lines changed

5 files changed

+30
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ nbproject/private
22
*~
33
composer.lock
44
vendor
5+
.idea

nbproject/project.properties

Lines changed: 0 additions & 7 deletions
This file was deleted.

nbproject/project.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Singleton.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,33 @@
22

33
namespace ByJG\DesignPattern;
44

5-
use Exception;
6-
75
trait Singleton
86
{
9-
protected function __construct()
10-
{ }
7+
protected function __construct()
8+
{
9+
}
10+
11+
/**
12+
* @throws SingletonException
13+
*/
14+
final private function __clone()
15+
{
16+
throw new SingletonException('You can not clone a singleton.');
17+
}
1118

12-
final private function __clone()
13-
{
14-
throw new Exception('You can not clone a singleton.');
15-
}
16-
17-
/**
18-
* @return static
19-
*/
20-
public static function getInstance()
21-
{
22-
static $instances;
19+
/**
20+
* @return static
21+
*/
22+
public static function getInstance()
23+
{
24+
static $instances;
2325

2426
$calledClass = get_called_class();
2527

26-
if (!isset($instances[$calledClass]))
27-
{
28+
if (!isset($instances[$calledClass])) {
2829
$instances[$calledClass] = new $calledClass();
29-
}
30-
return $instances[$calledClass];
31-
}
30+
}
31+
return $instances[$calledClass];
32+
}
3233

3334
}

src/SingletonException.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ByJG\DesignPattern;
4+
5+
class SingletonException extends \Exception
6+
{
7+
8+
}

0 commit comments

Comments
 (0)