Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL|E_STRICT);
- class RuneSubject implements SplSubject {
- private $_observers;
- public function __construct() {
- $this->_observers = new SplObjectStorage();
- }
- public function attach(SplObserver $object) {
- $this->_observers->attach($object);
- }
- public function detach(SplObserver $object) {
- $this->_observers->detach($object);
- }
- public function notify() {
- foreach ($this->_observers as $o) $o->update($this);
- }
- }
- class RuneAccount extends RuneSubject {
- public $status;
- public function __construct() {
- parent::__construct();
- $this->attach(new RuneLogger());
- $this->attach(new RuneMailer());
- }
- public function save() {
- $this->notify();
- }
- }
- class RuneLogger implements SplObserver {
- public function update(SplSubject $subject) {
- //Update status in log table
- echo "Updating status in log table.\n";
- }
- }
- class RuneMailer implements SplObserver {
- public function update(SplSubject $subject) {
- switch (get_class($subject)) {
- case "RuneAccount":
- if ($subject->status == "Expired") {
- //send email: "account expired"
- echo "Sending account expired email.\n";
- }
- }
- }
- }
- $account = new RuneAccount();
- $account->status = "Expired";
- $account->save();
- /**
- * Outputs:
- * Updating status in log table
- * Sending account expired email.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement