Serialization helpers for Broadway

v3.0.0 2021-01-30 16:11 UTC

This package is auto-updated.

Last update: 2021-10-29 02:13:11 UTC


By Matthias Noback

Event-sourcing framework Broadway requires a lot of your objects (like domain events and often view models as well) to be serializable because they have to be stored in plain text and later be reconstituted.

Making an object serializable requires you to write lots of pretty simple code. Even though that code is simple, you are likely to make mistakes in it. It's also a bit boring to write. To alleviate the pain, this library helps you to make objects serializable with in a few simple steps.


Just run

composer require matthiasnoback/broadway-serialization


This library is very simple and it just works because of a few simple and yet assumptions:

  1. Serializable objects have at least one property. Properties can have any kind of scope.
  2. Serializable objects implements Broadway's Serializable interface.
  3. Properties contain scalar values, serializable objects, or arrays of serializable objects.


use Broadway\Serializer\Serializable;
use BroadwaySerialization\Serialization\AutoSerializable;

class SerializableObject implements SerializableInterface
    use AutoSerializable;

     * @var string
    private $foo;
     * @var SerializableObject
    private $bar;
     * @var SerializableObject[]
    private $bars = [];

    protected static function deserializationCallbacks()
        return [
            'bar' => ['SerializableObject', 'deserialize'],
            'bars' => ['SerializableObject', 'deserialize']

By implementing deserializationCallbacks() you can define callables that should be called in order to deserialize the provided data. The callable will be called once for single values or multiple times for arrays of values.


The Serializable trait depends on a little bit of setup. Before calling its deserialize() method, make sure you have properly set up a Reconstitute service, like this:

use BroadwaySerialization\Reconstitution\Reconstitution;
use BroadwaySerialization\Reconstitution\ReconstituteUsingInstantiatorAndHydrator;
use Doctrine\Instantiator\Instantiator;
use BroadwaySerialization\Hydration\HydrateUsingReflection;

    new ReconstituteUsingInstantiatorAndHydrator(
        new Instantiator(),
        new HydrateUsingReflection()

Note that this package ships with different implementations of the hydrator. Depending on your setup and preferences, you can also use the HydrateUsingClosure class. The former generally performs better than the HydrateUsingReflection.


When using this library, your personal performance will increase significantly. Of course, runtime performance will be worse (not noticeably though, unless you're actually deserializing millions of objects).