1 _observers[] = $sub; 9 }10 11 //外部统一调用12 public function trigger(){13 if(!empty($this->_observers)){14 foreach($this->_observers as $observer){15 $observer->update();16 } 17 }18 }19 }20 21 //观察者接口22 interface obserable{23 public function update();24 }25 26 //实现观察者27 class listen implements obserable{28 public function update(){29 echo 'now first time you need to do listen';30 }31 }32 33 class read implements obserable{34 public function update(){35 echo 'now first time you need to read';36 }37 }38 39 class speak implements obserable{40 public function update(){41 echo 'now first time you need to speak';42 }43 }44 45 class write implements obserable{46 public function update(){47 echo 'now first time you need to write';48 }49 }50 51 $transfer = new transfer();52 $transfer->register(new listen());53 $transfer->register(new read());54 $transfer->register(new speak());55 $transfer->register(new write());56 $transfer->trigger();