Skip to content

Commit e730ee6

Browse files
author
Dominik Liebler
committed
added Singleton
0 parents  commit e730ee6

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

Singleton/Singleton.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Created by JetBrains PhpStorm.
4+
* User: dominik
5+
* Date: 21.08.11
6+
* Time: 15:16
7+
* To change this template use File | Settings | File Templates.
8+
*/
9+
10+
namespace DesignPatterns;
11+
12+
/**
13+
* Singleton pattern
14+
*
15+
* Purpose:
16+
* - to have only one instance of this object in the application, that handles all calls
17+
*
18+
* Examples:
19+
* - DB Connector
20+
* - Logger (May also be a Multiton if there are many Logfiles for several purposes)
21+
* - Lock file for the application (there is only one in the filesystem ...)
22+
*
23+
*
24+
*/
25+
class Singleton
26+
{
27+
/**
28+
* @var \DesignPatterns\Singleton
29+
*/
30+
private static $_instance;
31+
32+
/**
33+
* gets the instance via lazy initialization (created on first usage)
34+
*
35+
* @return Singleton
36+
*/
37+
public function getInstance()
38+
{
39+
if (null === self::$_instance) {
40+
self::$_instance = new self();
41+
}
42+
43+
return self::$_instance;
44+
}
45+
46+
/**
47+
* is not allowed to call from outside: private!
48+
*
49+
*/
50+
private function __construct()
51+
{}
52+
53+
/**
54+
* prevent the instance from being cloned
55+
*
56+
* @return void
57+
*/
58+
private function __clone()
59+
{}
60+
61+
/**
62+
* prevent from being unserialized
63+
*
64+
* @return void
65+
*/
66+
private function __wakeup()
67+
{}
68+
}

0 commit comments

Comments
 (0)