CodeBlanks Logo Icon CodeBlanks

The Observer pattern

PHP course · Advanced · Lesson 83/92

Learn the Observer pattern where a subject notifies multiple observers.

What does the Observer pattern do?

The Observer pattern lets a subject notify multiple observers about state changes. PHP ships built-in SplSubject and SplObserver interfaces for this pattern.

The subject adds observers with attach, removes them with detach, and notifies all of them with notify. Each observer is informed through its update method. The subject notifies observers without knowing their details.

The observer's update method

Every class implementing SplObserver must write the update(SplSubject $s) method. When the subject calls notify, each registered observer's update method runs in turn.

Inside, you can read the subject's new state, print a message or update another object. This method is the only link between subject and observer; the rest of the details stay inside the observer.

Exercise Write subject and observer, then notify

Write a ConcreteSubject class implementing \SplSubject with attach, detach, notify methods. Write a ConcreteObserver class implementing \SplObserver; in update it prints its name and ' updated'. Add the observer, call notify and print the result.

Show example solution
Write ConcreteSubject implements \SplSubject; add attach/detach/notify. Write ConcreteObserver implements \SplObserver; in update print name + ' updated'. Add with attach(new ConcreteObserver('Watcher')) and call notify().
Example solution:
<?php
class ConcreteSubject implements \SplSubject {
    private array $observers = [];
    public function attach(\SplObserver $o): void { $this->observers[] = $o; }
    public function detach(\SplObserver $o): void {
        $this->observers = array_values(array_filter($this->observers, fn($x) => $x !== $o));
    }
    public function notify(): void {
        foreach ($this->observers as $o) { $o->update($this); }
    }
}
class ConcreteObserver implements \SplObserver {
    public function __construct(private string $name) {}
    public function update(\SplSubject $s): void {
        echo $this->name . ' updated';
    }
}
$s = new ConcreteSubject();
$s->attach(new ConcreteObserver('Watcher'));
$s->notify();

Test yourself

  1. Match the Observer concepts

    SplSubject
    the subject
    SplObserver
    the observer
    attach
    adds an observer
    notify
    notifies all observers
  2. What happens when the subject calls notify?

    • The subject class is removed from memory
    • A new observer is added automatically
    • The subject is closed and exits
    • Each observer's update method is called
    Show answer

    Each observer's update method is called — Correct. notify calls the update method of every registered observer.

  3. The observer's notification method

    <?php
    class ConcreteObserver implements \SplObserver {
        public function __construct(private string $name) {}
        public function ____(\SplSubject $s): void {
            echo $this->name . " updated";
        }
    }
    
    Show answer
    <?php
    class ConcreteObserver implements \SplObserver {
        public function __construct(private string $name) {}
        public function update(\SplSubject $s): void {
            echo $this->name . " updated";
        }
    }
    

All lessons in Advanced