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

Singleton::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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