Repository navigation
Expand file tree
/
Copy pathSingletonTrait.php
More file actions
executable file
·77 lines (67 loc) · 1.76 KB
/
Copy pathSingletonTrait.php
File metadata and controls
executable file
·77 lines (67 loc) · 1.76 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* Webiny Framework (http://www.webiny.com/framework)
*
* @link http://www.webiny.com/wf-snv for the canonical source repository
* @copyright Copyright (c) 2009-2013 Webiny LTD. (http://www.webiny.com)
* @license http://www.webiny.com/framework/license
* @package WebinyFramework
*/
namespace Webiny\Component\StdLib;
/**
* SingletonTrait is a helper, once implemented on a class, automatically adds the singleton pattern to that class.
*
* @package Webiny\Component\StdLib
*/
trait SingletonTrait
{
protected static $wfInstance;
/**
* Return the current instance.
* If instance doesn't exist, a new instance will be created.
*
* @return $this
*/
final public static function getInstance()
{
if (isset(static::$wfInstance)) {
return static::$wfInstance;
} else {
static::$wfInstance = new static;
static::$wfInstance->init();
return static::$wfInstance;
}
}
/**
* Deletes the current singleton instance.
*/
final public static function deleteInstance()
{
static::$wfInstance = null;
}
/**
* The constructor is set to private to prevent creating new instances.
* If you want to fire a function after the singleton instance is created, just implement 'init' method into your class.
*/
final private function __construct()
{
}
/**
* Override this if you wish to do some stuff once the singleton instance has been created.
*/
protected function init()
{
}
/**
* Declare it as private.
*/
final private function __wakeup()
{
}
/**
* Declare it as private.
*/
final private function __clone()
{
}
}