Implementing the Publish-Subscribe (Pub-Sub) pattern in PHP involves creating a system where publishers send messages (events) to a central event bus, and subscribers receive and handle those events. This pattern decouples the publishers from the subscribers, enabling a flexible and scalable architecture. Let's explore the steps to implement the Pub-Sub pattern in PHP:
Event Bus:
Start by creating an event bus, which acts as a central hub for event communication. It manages the registration of subscribers and dispatches events to the appropriate subscribers. The event bus can be implemented as a singleton or a dependency-injected service.
class EventBus {
private $subscribers = [];
public function subscribe(string $eventName, $subscriber, string $methodName) {
$this->subscribers[$eventName][] = [$subscriber, $methodName];
}
public function publish(string $eventName, $data = null) {
if (isset($this->subscribers[$eventName])) {
foreach ($this->subscribers[$eventName] as [$subscriber, $methodName]) {
$subscriber->$methodName($data);
}
}
}
}
Publishers:
Create publisher classes responsible for emitting events to the event bus. Publishers encapsulate the logic for generating events and publishing them to the event bus. They can be implemented as separate classes or within existing application components.
class OrderPublisher {
private $eventBus;
public function __construct(EventBus $eventBus) {
$this->eventBus = $eventBus;
}
public function createOrder($orderData) {
// Order creation logic...
// Publish 'order.created' event
$this->eventBus->publish('order.created', $orderData);
}
}
Subscribers:
Create subscriber classes that handle specific events. Subscribers register themselves with the event bus to receive events of interest. They define methods that will be invoked when the corresponding events are published.
class EmailNotificationSubscriber {
public function sendOrderCreatedNotification($orderData) {
// Email notification logic...
}
}
Register Subscribers:
In your application initialization phase, register subscribers with the event bus to start receiving events. This can be done in a central configuration file or within the dependency injection container.
$eventBus = new EventBus();
$emailSubscriber = new EmailNotificationSubscriber();
$eventBus->subscribe('order.created', $emailSubscriber, 'sendOrderCreatedNotification');
Publishing Events:
Invoke the publisher's methods to trigger events within the application. The events will be published to the event bus, which will notify the subscribed subscribers.
$orderPublisher = new OrderPublisher($eventBus);
$orderPublisher->createOrder($orderData);
By following these steps, you can implement the Pub-Sub pattern in PHP. This decoupled architecture allows publishers and subscribers to operate independently and promotes scalability and flexibility in your application.
Additionally, you can extend the implementation by adding features such as event filtering, event handlers, or prioritization based on your application requirements.