0
0
PHPprogramming~10 mins

Observer pattern in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Observer pattern
Subject state changes
Notify all observers
Each observer updates
Observers react to change
End
The subject changes state, then notifies all observers, which then update themselves accordingly.
Execution Sample
PHP
<?php
class Subject {
  private $observers = [];
  private $state;

  public function attach($observer) {
    $this->observers[] = $observer;
  }

  public function setState($state) {
    $this->state = $state;
    $this->notify();
  }

  public function notify() {
    foreach ($this->observers as $obs) {
      $obs->update($this->state);
    }
  }
}

class Observer {
  public function update($state) {
    echo "Observer notified with state: $state\n";
  }
}

$subject = new Subject();
$observer1 = new Observer();
$observer2 = new Observer();
$subject->attach($observer1);
$subject->attach($observer2);
$subject->setState('ON');
?>
This PHP code shows a subject notifying two observers when its state changes.
Execution Table
StepActionSubject StateObservers NotifiedOutput
1Create Subjectnullnone
2Create Observer1nullnone
3Create Observer2nullnone
4Attach Observer1nullnone
5Attach Observer2nullnone
6Set Subject state to 'ON'ONObserver1, Observer2Observer notified with state: ON Observer notified with state: ON
7Notify observersONObserver1, Observer2
8Observer1 update calledONObserver1Observer notified with state: ON
9Observer2 update calledONObserver2Observer notified with state: ON
10EndONall notifiedNotifications complete
💡 All observers notified after subject state changed to 'ON'
Variable Tracker
VariableStartAfter Step 5After Step 6After Step 10
subject->statenullnull'ON''ON'
subject->observers[][Observer1, Observer2][Observer1, Observer2][Observer1, Observer2]
Key Moments - 3 Insights
Why do observers get notified only after the subject's state changes?
Because the notify() method is called inside setState(), so observers update only when the state changes (see execution_table step 6).
Does attaching observers change the subject's state?
No, attaching observers just adds them to the list; the state remains unchanged until setState() is called (see steps 4 and 5).
Why does each observer print the notification message separately?
Because notify() calls update() on each observer individually, so each observer reacts and outputs its own message (see steps 8 and 9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the subject's state after step 5?
A'ON'
Bnull
Cempty string
Dundefined
💡 Hint
Check the 'Subject State' column at step 5 in the execution_table.
At which step do observers start getting notified?
AStep 5
BStep 7
CStep 6
DStep 10
💡 Hint
Look at the 'Observers Notified' column and output messages in execution_table.
If we add a third observer before step 6, how would the 'Observers Notified' column change at step 7?
AObserver1, Observer2, Observer3
BObserver1, Observer2
CObserver3 only
DNo observers
💡 Hint
The subject notifies all attached observers, so adding one more means all three get notified.
Concept Snapshot
Observer pattern:
- Subject holds list of observers
- Observers attach to subject
- When subject state changes, it calls notify()
- notify() calls update() on each observer
- Observers react to changes independently
Full Transcript
The Observer pattern lets one object (subject) tell many others (observers) when something changes. Here, the subject keeps a list of observers. When its state changes, it calls notify(), which calls update() on each observer. Each observer then reacts, for example by printing a message. This way, observers stay updated automatically without the subject needing to know details about them.