status; } /** * (non-PHPdoc) * @see Observable::attach() */ public function attach(Observer $instance) { foreach ($this->observers as $observer) { if ($instance === $observer) { return false; } } $this->observers[] = $instance; } /** * (non-PHPdoc) * @see Observable::detach() */ public function detach(Observer $instance) { foreach ($this->observers as $key => $observer) { if ($instance === $observer) { unset($this->observers[$key]); } } } /** * (non-PHPdoc) * @see Observable::notify() */ public function notify() { foreach ($this->observers as $observer) { $observer->update($this); } } /** * Save comment's data * * @return void */ public function save() { // Save comment's data into database or file if (1) { $this->status = self::SAVED_SUCCESS; } $this->notify(); } } class Logger implements Observer { /** * @param string $message */ private function log($message) { echo __CLASS__ . ' : ' . $message; } /** * @param Observable $subject */ public function update(Observable $subject) { if ($subject->getStatus() == Comment::SAVED_SUCCESS) { $this->log("Comment saved successfully\n", Comment::SAVED_SUCCESS); } } } /** * */ class Mailer implements Observer { /** * @param string $message */ private function send($message) { echo __CLASS__ . ' : ' . $message; } /** * @param Observable $subject */ public function update(Observable $subject) { if ($subject->getStatus() == Comment::SAVED_SUCCESS) { $this->send("Comment saved successfully\n", Comment::SAVED_SUCCESS); } } } $comment = new Comment(); $comment->attach(new Logger); $comment->attach(new Mailer); $comment->save(); ?>