-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseSingleton.php
More file actions
43 lines (37 loc) · 1.02 KB
/
BaseSingleton.php
File metadata and controls
43 lines (37 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace App\DesignPattern\Singleton;
/**
* This singleton supports multiple singletons in the app.
* It defines the base features of singleton and subclasses
* can implement the main logic
*/
class BaseSingleton
{
/**
* Instance of the each subclass is store in array
* @var $instance array
*/
private static $instance = [];
/**
* Actual singleton class constructor should be private
* but since we are going to use this as a base class
* so that why we are making it protected
*/
protected function __construct() {}
/**
* Cloning and serialization not permitted
*/
protected function __clone() { }
/**
* This method will be used to get instance of the class
*
* @return $instance
*/
public static function getInstance() {
$subClass = static::class;
if (!isset(self::$instance[$subClass])) {
self::$instance[$subClass] = new static();
}
return self::$instance[$subClass];
}
}