Completed
Push — master ( faa8b2...2212f7 )
by Joao
01:49
created

Singleton::__sleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace ByJG\DesignPattern;
4
5
trait Singleton
6
{
7
    protected function __construct()
8
    {
9
    }
10
11
    /**
12
     * @throws SingletonException
13
     */
14
    final public function __clone()
15
    {
16
        throw new SingletonException('You can not clone a singleton.');
17
    }
18
19
    /**
20
     * @throws SingletonException
21
     */
22
    final public function __sleep()
23
    {
24
        throw new SingletonException('You can not serialize a singleton.');
25
    }
26
27
    /**
28
     * @throws SingletonException
29
     */
30
    final public function __wakeup()
31
    {
32
        throw new SingletonException('You can not deserialize a singleton.');
33
    }
34
35
    /**
36
     * @return static
37
     */
38
    public static function getInstance()
39
    {
40
        static $instances;
41
42
        $calledClass = get_called_class();
43
44
        if (!isset($instances[$calledClass])) {
45
            $instances[$calledClass] = new $calledClass();
46
        }
47
        return $instances[$calledClass];
48
    }
49
50
}
51