This package is abandoned and no longer maintained. No replacement package was suggested.

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

3.2.7 2018-04-18 22:40 UTC


Build Status codecov

This package provides a quick way to define structured objects (DTOs) using the JSON Schema standard.

A Data Transfer Object (DTO) is an object used to pass typed data between layers in your application, similiar in concept to Structs in C, Martin Fowler's Transfer Objects, or Value Objects.

DTOs are a helpful counterpart to the Data Accessor Object (DAO) or Repository patterns.

The functionality is similar to what is offered by the shape package.


Define a PHP object using JSON Schema syntax:

class ExampleObject extends Dto\Dto
    protected $schema = [
        'type' => 'object',
        'properties' => [
            'a' => ['type' => 'string'],
            'b' => ['type' => 'integer']
        'additionalProperties' => false

Now you can instantiate and use your object:

// Assume you have included vendor/autoload.php and the above ExampleObject class
$obj = new ExampleObject();
$obj->a = "Some String";
$obj->b = 123; // this is defined as an integer, so values WILL be type-cast to integer!
$obj->c = 'Whoops, this will throw an Exception because additionalProperties are not allowed';

Or reference a JSON Schema directly:

class ExampleObject extends Dto\Dto
    protected $schema = [
        '$ref' => 'http://example.com/some/schema.json'

Possible Uses

  • In APIs: Consume a JSON Schema API at runtime without needing to parse data formats.
  • In Views: Instead of passing your view layer just any array, you can define a DTO and use type-hinting to ensure that your views will always have the data attributes they need.
  • For Caching: Instead of relying on your own haphazard convention, you can store and retrieve a specific DTO class from cache without having to guess which attributes or array keys are available.
  • Service Classes: when your service class expects to operate on a specific type of data.
  • Result Sets: instead of returning an array of stdClass objects or associative arrays from a database lookup, a DTO can describe the result set as well as the individual records.

Read more in the DTO Wiki


Version History


  • Fixed validation issue when PHP classes were referenced in schemas using the $ref keyword.


  • Removed unused dependency on webmozart/json


  • Performance boost by eliminating redundant validation.


  • strval() conversion inside __toString() method: makes for easier UX when checking object properties.


  • Support for nested relative schema paths when reading JSON schemas.


  • Tests added for serialize method.


  • Includes the $baseDir and getBaseDir() methods in order to support relative paths to JSON schemas as used by the $ref keyword.


  • Includes proper support for in-line definitions and inheritance of them.


  • Integration of the JSON Schema 4 specification to drive all structure and type definitions.


We don't speak of version 2.


  • Fixes for anonymous hashes of DTOs injected into the constructor.
  • Corrected namespaces in tests.


Fixes for arrays of DTOs injected into the constructor as arrays.


Fixes for hierarchical data injected into the constructor.


Initial release with some grooming as documentation was added.