-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUser.php
49 lines (43 loc) · 1006 Bytes
/
User.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
45
46
47
48
49
<?php
/**
* Created by PhpStorm.
* User: zcer
* Date: 2020/2/23
* Time: 下午4:44
*/
/**
* User implements the observed object (called Subject), it maintains a list of observers and sends notifications to
* them in case changes are made on the User object
*/
class User implements SplSubject
{
private $email;
/**
* @var SplObjectStorage
*/
private $observers;
public function __construct()
{
$this->observers = new SplObjectStorage();
}
public function attach(SplObserver $observer)
{
$this->observers->attach($observer);
}
public function detach(SplObserver $observer)
{
$this->observers->detach($observer);
}
public function changeEmail(string $email)
{
$this->email = $email;
$this->notify();
}
public function notify()
{
/** @var SplObserver $observer */
foreach ($this->observers as $observer) {
$observer->update($this);
}
}
}