Completed
Push — master ( 5f1ca1...a7b5ff )
by Henry
04:31
created

Singleton   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 63
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __clone() 0 3 1
A getInstance() 0 12 2
A clearInstance() 0 5 1
1
<?php
2
namespace Redaxscript;
3
4
use function array_key_exists;
5
6
/**
7
 * abstract class to create a singleton class
8
 *
9
 * @since 2.2.0
10
 *
11
 * @package Redaxscript
12
 * @category Singleton
13
 * @author Henry Ruhs
14
 *
15
 * @codeCoverageIgnore
16
 */
17
18
abstract class Singleton
19
{
20
	/**
21
	 * array of static instances
22
	 *
23
	 * @var array
24
	 */
25
26
	protected static $_instanceArray = [];
27
28
	/**
29
	 * constructor of the class
30
	 *
31
	 * @since 2.2.0
32
	 */
33
34
	private function __construct()
35
	{
36
	}
37
38
	/**
39
	 * clone of the class
40
	 *
41
	 * @since 5.0.0
42
	 */
43
44
	private function __clone()
45
	{
46
	}
47
48
	/**
49
	 * get the instance
50
	 *
51
	 * @since 5.0.0
52
	 *
53
	 * @return static
54
	 */
55
56
	public static function getInstance()
57
	{
58
		$className = static::class;
59
60
		/* create instance */
61
62
		if (!array_key_exists($className, static::$_instanceArray))
63
		{
64
			static::$_instanceArray[$className] = new static();
65
		}
66
		return static::$_instanceArray[$className];
67
	}
68
69
	/**
70
	 * clear the instance
71
	 *
72
	 * @since 3.0.0
73
	 */
74
75
	public static function clearInstance() : void
76
	{
77
		$className = static::class;
78
		self::$_instanceArray[$className] = null;
79
	}
80
}
81