<meta charset= ' UTF-8 '/><title> Observer pattern </title><?php class Tongzhi implements Splsubject { Protected $subs = Array (); Public $username = ' I am the Observer '; Public Function __construct () {} public Function attach (Splobserver $sub) {$this->subs [] = $sub; The Public function detach (Splobserver $sub) {$keys = Array_keys ($this->subs, $sub); $key = $keys [0]; unset ($this->subs[$key]); } public Function Notify () {foreach ($this->subs as $value) {$value->u Pdate ($this); }}} class Obs1 implements Splobserver {public Function update (Splsubject $tz) {echo $ Tz->username. ', I am the Observer 1<br/> '; }} class OBS2 implements Splobserver {public Function update (Splsubject $tz) {echo $tz->us Ername. ', I am the Observer 2<br/> '; }} ClassOBS3 implements Splobserver {public Function update (Splsubject $tz) {echo $tz->username. ', I am the Observer 3& Lt;br/> '; }} $tz = new Tongzhi; $tz->attach (new OBS1); $tz->attach (new OBS2); $tz->detach (new OBS2); $tz->attach (new OBS3); $tz->notify ();? >
PHP Implementation Viewer mode