Completed
Pull Request — master (#6)
by Patrick
03:47
created

Singleton::__clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Singleton class
4
 *
5
 * This file describes the Singleton parent class
6
 *
7
 * PHP version 5 and 7
8
 *
9
 * @author Patrick Boyd / [email protected]
10
 * @copyright Copyright (c) 2015, Austin Artistic Reconstruction
11
 * @license http://www.apache.org/licenses/ Apache 2.0 License
12
 */
13
14
/**
15
 * A class that only allows a single instance to be created.
16
 *
17
 * This class only allows a single instance to be created. This is especially useful for 
18
 * database and other abstractions to reduce the number of connections. 
19
 */
20
class Singleton
21
{
22
    /**
23
     * Return the instance of the object
24
     *
25
     * This function returns the object instance if it exists and if not it will create a new copy
26
     */
27
    public static function getInstance()
28
    {
29
        static $instances = array();
30
        $class = get_called_class();
31
        if(!isset($instances[$class]))
32
        {
33
            $instances[$class] = new static();
34
        }
35
        return $instances[$class];
36
    }
37
38
    /**
39
     * A singleton constructor should not be publically accessible. All callers should use getInstance()
40
     */
41
    protected function __construct()
42
    {
43
    }
44
45
    /**
46
     * A singleton can not be cloned
47
     */
48
    private function __clone()
49
    {
50
    }
51
52
    /**
53
     * A singleton can not be serialized and deserialized
54
     * 
55
     * @SuppressWarnings("UnusedPrivateMethod")
56
     */
57
    private function __wakeup()
58
    {
59
    }
60
}
61
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
62
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
63