Event Sourcing is a pattern where every change in application state is captured as an immutable event. This allows full auditability and the ability to rebuild state.

How Event Sourcing Works

  • All domain events are persisted in an event store
  • The current state is derived by replaying events
  • Supports audit, history, and time travel debugging

Example Event

class UserRegistered {
    public function __construct(public int $userId, public string $email) {}
}

Event Store

An event store saves events in sequence:

interface EventStore {
    public function append(object $event): void;
    public function load(string $aggregateId): array;
}

Benefits

  • Audit trail and full history of changes
  • Supports reactive architectures
  • Can rebuild state after bug fixes or migrations

Conclusion

Event Sourcing with PHP is ideal for complex domains, enabling reactive, auditable, and maintainable systems.