forked from jkapuscik2/design-patterns-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton.php
More file actions
37 lines (27 loc) · 721 Bytes
/
singleton.php
File metadata and controls
37 lines (27 loc) · 721 Bytes
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
<?php
namespace Creational\Singleton;
class ActiveUser {
private $email;
private static $instance;
private function __construct () {
// Email is retrieved from a db
$this->email = "active@user.com";
}
private function __clone () {
}
public function setName (string $email): void {
$this->email = $email;
}
public function changeEmail (): string {
return $this->email;
}
private static function get (): ActiveUser {
return new ActiveUser();
}
public static function getInstance (): ActiveUser {
if (!self::$instance) {
self::$instance = self::get();
}
return self::$instance;
}
}