-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobservice.php
More file actions
82 lines (60 loc) · 1.52 KB
/
Copy pathobservice.php
File metadata and controls
82 lines (60 loc) · 1.52 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/3/1
* Time: 14:44
*/
//被观察者
class user implements SplSubject{
public $lognum;
public $hobby;
protected $obervers;
public function __construct(){
$this->lognum=rand(1,10);
$hobby=array('sports','feed');
shuffle($hobby);
$this->hobby=$hobby;
$this->observers = new SplObjectStorage();
}
public function login(){
$this->notify();
}
public function attach(SplObserver $observer){
$this->observers->attach($observer);
}
public function detach(SplObserver $observer){
$this->observers->detach($observer);
}
public function notify()
{
$this->observers->rewind();
while ($this->observers->valid()) {
$observer = $this->observers->current();
$observer->update($this);
$this->observers->next();
}
}
}
class secrity implements SplObserver{
public function update(SplSubject $subject){
if($subject->lognum<3){
echo '这是第'.$subject->lognum.'次安全登录';
}else{
echo '这是第'.$subject->lognum.'次异常登录';
}
}
}
class ad implements SplObserver{
public function update(SplSubject $subject){
if('sports'==$subject->hobby[0]){
echo 'sports';
}else{
echo 'feed';
}
}
}
$user = new user();
$user->attach(new secrity());
$user->attach(new ad());
$user->login();