| 1 | <?php |
||
| 16 | class Singleton extends NotConstructable |
||
|
|
|||
| 17 | {
|
||
| 18 | /** |
||
| 19 | * @var array $instances |
||
| 20 | */ |
||
| 21 | protected static $instances = [ ]; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Prevent class from being constructed. |
||
| 25 | */ |
||
| 26 | private function __construct() {}
|
||
| 27 | |||
| 28 | /** |
||
| 29 | * Get instance from $instances array. |
||
| 30 | * |
||
| 31 | * If the called class is present in the static $instances array, then return it. |
||
| 32 | * Otherwise, create a new copy of the class and store it in the $instances |
||
| 33 | * array. |
||
| 34 | * |
||
| 35 | * @return mixed |
||
| 36 | */ |
||
| 37 | 3 | final public static function getInstance($calledClass = null) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Destroy |
||
| 50 | * |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | 1 | final public static function destroy() |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @param $instance |
||
| 60 | */ |
||
| 61 | 1 | final public static function destroyInstance($instance) |
|
| 65 | } |
||
| 66 |