The Observer pattern in PHP is a behavioral design pattern that allows an object, known as the subject, to maintain a list of dependents, called observers, and notify them of any state changes, usually by calling one of their methods. This pattern is useful when you need a way to update multiple objects when the state of another object changes, without tightly coupling the objects together.
Key Concepts:
- Subject: The object that tracks its state and informs observers about changes.
- Observer: The object that needs to be notified about changes in the subject.
Let’s consider a scenario where we have a blog, and we want to notify subscribers whenever a new post is published. Here, the blog is the subject, and the subscribers are the observers. We are explaining their implementation step by step here:
1. First we create an Observer Interface in which we define the ”update” method that needs to be implemented by all concrete observers.
<?php
interface Observer {
public function update($postTitle);
}
2. Then we create a “Subscriber” class that implements the ”update” method to define what happens when the subscriber is notified about a new post.
<?php
class Subscriber implements Observer {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function update($postTitle) {
echo $this->name . " has been notified about a new post: " . $postTitle . "";
}
}
3. After that, we will create “Subject” Interface which defines methods to attach, detach, and notify observers.
<?php
interface Subject {
public function attach(Observer $observer);
public function detach(Observer $observer);
public function notify();
}
4. Now, create a subject class “Blog” that implements the “Subject” interface and maintains a list of subscribers. When a new post is added using the `addPost` method, it notifies all subscribers.
<?php
class Blog implements Subject {
private $subscribers = [];
private $latestPost;
public function attach(Observer $observer) {
$this->subscribers[] = $observer;
}
public function detach(Observer $observer) {
$index = array_search($observer, $this->subscribers);
if ($index !== false) {
unset($this->subscribers[$index]);
}
}
public function notify() {
foreach ($this->subscribers as $subscriber) {
$subscriber->update($this->latestPost);
}
}
public function addPost($postTitle) {
$this->latestPost = $postTitle;
$this->notify();
}
}
5. Using the below code, we are creating subscribers, attaching them to the blog, and adding a new post to trigger notifications.
<?php
$subscriber1 = new Subscriber("Alice");
$subscriber2 = new Subscriber("Bob");
$subscriber3 = new Subscriber("Charlie");
// Creating subject (blog)
$blog = new Blog();
// Attaching subscribers to the blog
$blog->attach($subscriber1);
$blog->attach($subscriber2);
$blog->attach($subscriber3);
// Adding a new post, which triggers notifications
$blog->addPost("Understanding the Observer Pattern in PHP");
As demonstrated in the example above, subscribers can be added or removed without altering the blog class. This promotes loose coupling, making the system more flexible and easier to maintain. When a new post is published, all subscribers are automatically notified, ensuring they remain up-to-date with the latest content.