-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleton_Pattern.php
44 lines (35 loc) · 1010 Bytes
/
Singleton_Pattern.php
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
// My version of the Singleton Pattern in php.
// Description of pattern found at: https://en.wikipedia.org/wiki/Singleton_pattern
<?php
class Sky {
private static $instanceCount = 0;
private $id;
protected static $_instance;
protected function __construct() {
self::$instanceCount += 1;
$this->id = self::$instanceCount;
}
private function __clone() {}
private function __sleep() {}
private function __wakeup() {}
public function __toString() {
return "I am sky object {$this->id} of " .
self::$instanceCount .
" total instances.\n";
}
public static function getInstance() {
if(is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
}
$sky = Sky::getInstance();
echo $sky;
$anotherSky = Sky::getInstance();
echo $anotherSky;
$bloodRedSky = Sky::getInstance();
echo $bloodRedSky;
$pinkSky = Sky::getInstance();
echo $pinkSky;
?>