Events In Practice

GlasgowPHP - May 2017


Scott Pringle

PHP Developer @ People's Postcode Lottery

DDDEdinburgh Organiser

What Is An Event?

An event is an action recognized by software, that may be handled by the software

An Event is the encapsulation of a recorded action within an application

Me, probably plagiarising someone...


namespace Demo;

class UserRegistered
    private $user;

    public function __construct(User $user)
        $this->user = $user;

    public function user(): User
        return $this->user;

Events have 0 or more listeners


namespace Demo;

class UserRegisteredListener
    private $emailer;

    public function __construct(Emailer $emailer)
        $this->emailer = $emailer;

    public function onUserRegistered(UserRegistered $event)    

Simple, right?

Thinking In Events

As Developers we...

  • Think in Actions
  • Something "will" happen


  • Video Subscription Platform (E.G. Netflix)
    • Trials Are Available
    • Payments are not taken instantly
      • Payments get authorised & scheduled

User Registers

Add payment details

authorise payments

Add Subscription

Actions not events

thinking in events is hard!

Mindset change from "will" to "has"

Thinking in reactions, not actions

Event Driven Architecture

4 Principal Layers

  • Event Generator
  • Event Channel
  • Event Processing
  • Downstream Activity

Event Generator

  • Senses A Change
  • Frames The Change As An Event

Event Channel

  • Middleware from Event Generator
    to Event Processor
  • TCP/IP connection / Input file
  • Stores events into queues for Processors

Event Processing

  • Where the magic happens
  • 0 or more reactions
  • Could live outside application

Downstream Activity

  • Consequence of Event Processing, e.g.
    • Order confirmed emails being sent
    • Stock count being updated

Events in Frameworks & Libraries

Symfony HTTP Kernel

  • kernel.request
  • kernel.controller
  • kernel.view
  • kernel.response
  • kernel.terminate
  • kernel.exception


  • preRemove
  • postRemote
  • prePersist
  • postPersist
  • preUpdate
  • postUpdate
  • ...

All Of These Are Events Which Can Be Subscribed To / Published

I can't build an app using pre-defined events?!

Why not?

Custom Hooks Are Available

<?xml version="1.0" encoding="UTF-8"?>

<doctrine-mapping xmlns=""

    <entity name="User">

            <lifecycle-callback type="prePersist" method="doStuffOnPrePersist"/>
            <lifecycle-callback type="postPersist" method="doStuffOnPostPersist"/>



namespace Demo;

class User
    // All the properties

    public function doStuffOnPrePersist()
        $this->persisted = true;

    public function doStuffOnPostPersist()
        $this->persisted = false;

I want more control

That I Can Agree With!

  • More explicit the events, the better
  • Control exactly where an event is triggered
    • Which controller, console command etc
  • Events mean more to the business

namespace Demo;

class UserRegisterController
    private $eventDispatcher;

    public function __construct(EventDispatcherInterface $eventDispatcher)
        $this->eventDispatcher = $eventDispatcher;

    public function __invoke(Request $request)
        $user = new User(...);

            new UserRegistered(

        return new JsonResponse();
<!-- app/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns=""

        <service id="app.event.user_registered" class="Demo\EventListener\UserRegistered">
            <tag name="kernel.event_listener" event="event.user_registered" method="onUserRegistered" />

I've probably got that wrong...

Events Can be Used as Messages

Events can be handled asynchronously

Events should be immutable

Tell other parts of your application to do something

Create an audit trail...

Tell other applications to do something

  • Events Can Be Streamed
    • Kafka
    • RabbitMQ
    • Any Other Queuing System
    • File system

Event Sourcing

Events used to create state

No more relational tables

State becomes rebuildable

Predict state with Mocked events

Complete application history

You don't think of an audit log until you're audited

Treat events As first class citizens

Driven by Problem Domains

Map to things that really happen in your organisation

Business People don't care about technology

They only care about results


Thank you

Twitter: @Luciam91

Events In Practice

By Scott Pringle

Loading comments...

More from Scott Pringle