-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.php
More file actions
59 lines (52 loc) · 1.16 KB
/
Singleton.php
File metadata and controls
59 lines (52 loc) · 1.16 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace Safronik\CodePatterns\Generative;
/**
* Singleton
*
* @author Roman safronov
* @version 1.0.0
*/
trait Singleton
{
/**
* @var mixed
*/
protected static self $instance;
// Constructor is not allowed
public function __clone() {}
public function __wakeup() {}
/**
* Constructor
*
* @param array $params Additional parameters to pass in the method initialize()
*
* @return mixed|\static
*/
public static function getInstance( ...$params ): mixed
{
return self::$instance ?? self::$instance = new static( ...$params );
}
/**
* Alternative constructor
* Doesn't return anything just initiate object
*
* Could be useful in case we don't need the object right now
*
* @param ...$params
*
* @return void
*/
public static function initialize( ...$parameters ): void
{
self::getInstance( ...$parameters );
}
/**
* Checks if the object is initialized
*
* @return bool
*/
public static function isInitialized(): bool
{
return isset( static::$instance );
}
}