1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Spl\Patterns\Creational\Singleton;
|
15
|
|
|
|
16
|
|
|
/**
|
17
|
|
|
* Class AbstractSingleton
|
18
|
|
|
* @package O2System\Spl\Patterns\Creational\Singleton
|
19
|
|
|
*/
|
20
|
|
|
class AbstractSingleton implements InstanceInterface
|
21
|
|
|
{
|
22
|
|
|
/**
|
23
|
|
|
* AbstractSingleton::$instance
|
24
|
|
|
*
|
25
|
|
|
* Singleton Instance
|
26
|
|
|
*
|
27
|
|
|
* @var static
|
28
|
|
|
*/
|
29
|
|
|
protected static $instance;
|
30
|
|
|
|
31
|
|
|
// ------------------------------------------------------------------------
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
* AbstractSingleton::__construct
|
35
|
|
|
*
|
36
|
|
|
* Protected constructor to prevent creating a new instance of the
|
37
|
|
|
* *Singleton* via the `new` operator from outside of this class.
|
38
|
|
|
*/
|
39
|
|
|
protected function __construct()
|
40
|
|
|
{
|
41
|
|
|
static::$instance =& $this;
|
42
|
|
|
}
|
43
|
|
|
|
44
|
|
|
// ------------------------------------------------------------------------
|
45
|
|
|
|
46
|
|
|
/**
|
47
|
|
|
* AbstractSingleton::getInstance
|
48
|
|
|
*
|
49
|
|
|
* Returns the *Singleton* instance of this class.
|
50
|
|
|
*
|
51
|
|
|
* @return static Returns the instance class
|
52
|
|
|
*/
|
53
|
|
|
public static function getInstance()
|
54
|
|
|
{
|
55
|
|
|
if (empty(static::$instance)) {
|
56
|
|
|
$className = get_called_class();
|
57
|
|
|
|
58
|
|
|
static::$instance = new $className();
|
59
|
|
|
|
60
|
|
|
if (method_exists(static::$instance, '__reconstruct')) {
|
61
|
|
|
call_user_func_array([&static::$instance, '__reconstruct'], func_get_args());
|
62
|
|
|
}
|
63
|
|
|
}
|
64
|
|
|
|
65
|
|
|
return static::$instance;
|
|
|
|
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
// ------------------------------------------------------------------------
|
69
|
|
|
|
70
|
|
|
/**
|
71
|
|
|
* AbstractSingleton::__clone
|
72
|
|
|
*
|
73
|
|
|
* Application of __clone magic method with private visibility to prevent cloning of
|
74
|
|
|
* the *Singleton* instance.
|
75
|
|
|
*
|
76
|
|
|
* @return void
|
77
|
|
|
*/
|
78
|
|
|
protected function __clone()
|
79
|
|
|
{
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
// ------------------------------------------------------------------------
|
83
|
|
|
|
84
|
|
|
/**
|
85
|
|
|
* AbstractSingleton::__wakeup
|
86
|
|
|
*
|
87
|
|
|
* Application of __wakeup magic method with private visibiliry to prevent unserializing of
|
88
|
|
|
* the *Singleton* instance.
|
89
|
|
|
*
|
90
|
|
|
* @return void
|
91
|
|
|
*/
|
92
|
|
|
protected function __wakeup()
|
93
|
|
|
{
|
94
|
|
|
}
|
95
|
|
|
} |