<?php
Class User implements splsubject{
Public $loginNum;
Public $hobby;
public $observers = null;
Public function __construct ($hobby) {
$this->loginnum = rand (1,10);
$this->hobby = $hobby;
$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 () {
$this->observers->rewind ();
while ($this->observers->valid ()) {
$observer = $this->observers->current ();
$observer->update ($this);
$this->observers->next ();
}
}
Public Function login () {
$this->notify ();
}
}
Class Secrity implements splobserver{
Public Function Update (Splsubject $subject) {
echo "First". $subject->loginnum. " Second Landing ";
}
}
Class Ad implements splobserver{
Public Function Update (Splsubject $subject) {
echo "Hobby". $subject->hobby;
}
}
$user = New User ("basketball");
$user->attach (New secrity ());
$user->attach (New Ad ());
$user->login ();
PHP design mode (3)-Viewer mode