-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathUser.php
More file actions
48 lines (38 loc) · 858 Bytes
/
User.php
File metadata and controls
48 lines (38 loc) · 858 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
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
* Created by PhpStorm.
* User: 小粽子
* Date: 2018/12/19
* Time: 11:15
*/
namespace DesignPattern\Behavioral\Observer;
use SplObserver;
class User implements \SplSubject
{
public $observers;
protected $data = [];
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 notify()
{
foreach ($this->observers as $observer) {
$observer->update($this);
}
}
public function __set($name, $value)
{
$this->data[$name] = $value;
// 通知观察者用户被改变
$this->notify();
}
}