PHPackages                             php-collective/dto - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. php-collective/dto

ActiveLibrary

php-collective/dto
==================

Framework-agnostic Data Transfer Object library with code generation

0.1.15(1mo ago)246.9k—4%13MITPHPPHP ^8.2CI passing

Since Dec 14Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/php-collective/dto)[ Packagist](https://packagist.org/packages/php-collective/dto)[ Docs](https://github.com/php-collective/dto)[ RSS](/packages/php-collective-dto/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (14)Versions (39)Used By (3)

PHP Data Transfer Objects
=========================

[](#php-data-transfer-objects)

[![CI](https://github.com/php-collective/dto/actions/workflows/ci.yml/badge.svg)](https://github.com/php-collective/dto/actions/workflows/ci.yml)[![codecov](https://camo.githubusercontent.com/37b58cb64c44eb680766e6b8f5ca26d4867be87818dc7d24956ee30a69ae6af3/68747470733a2f2f636f6465636f762e696f2f67682f7068702d636f6c6c6563746976652f64746f2f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/php-collective/dto)[![PHPStan](https://camo.githubusercontent.com/f60d96f7c2579690ab6dfa8918f777fe93a02a92301c661eb38a85861a92b780/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230382d627269676874677265656e2e7376673f7374796c653d666c6174)](https://phpstan.org/)[![Latest Stable Version](https://camo.githubusercontent.com/ecd2fe04fcb8c092da91dafb1c3df899446ca3feed369c756e8348572ed78dd1/68747470733a2f2f706f7365722e707567782e6f72672f7068702d636f6c6c6563746976652f64746f2f762f737461626c652e737667)](https://packagist.org/packages/php-collective/dto)[![PHP](https://camo.githubusercontent.com/0f16581d1180dbfd4c0e13166ec1267d4ad2f2fab8281ea6d6b284cf5c65d921/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e322532422d626c75652e737667)](https://php.net)[![License](https://camo.githubusercontent.com/08cef40a9105b6526ca22088bc514fbfdbc9aac1ddbf8d4e6c750e3a88a44dca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c75652e737667)](LICENSE)

Framework-agnostic DTO library with **code generation** for PHP.

Unlike runtime reflection libraries, this library generates optimized DTO classes at build time, giving you:

- **Zero runtime reflection overhead**
- **Perfect IDE autocomplete** with real methods
- **Excellent static analysis** support (PHPStan/Psalm work out of the box)
- **Reviewable generated code** in pull requests
- **JSON Schema generation** for API documentation
- **Schema importer** to bootstrap DTOs from JSON data or OpenAPI specs

See [Motivation](https://php-collective.github.io/dto/guide/motivation) for why code generation beats runtime reflection.

Installation
------------

[](#installation)

```
composer require php-collective/dto
```

Quick Start
-----------

[](#quick-start)

Define DTOs in PHP (or XML, YAML, NEON):

```
// config/dto.php
use PhpCollective\Dto\Config\Dto;
use PhpCollective\Dto\Config\Field;
use PhpCollective\Dto\Config\Schema;

return Schema::create()
    ->dto(Dto::create('Car')->fields(
        Field::string('color'),
        Field::dto('owner', 'Owner'),
    ))
    ->dto(Dto::create('Owner')->fields(
        Field::string('name'),
    ))
    ->toArray();
```

Generate and use:

```
vendor/bin/dto generate
```

```
$car = CarDto::createFromArray(['color' => 'red']);
$car->setOwner(OwnerDto::create(['name' => 'John']));
$array = $car->toArray();
```

See the [documentation](https://php-collective.github.io/dto/) for detailed examples.

Immutable DTOs
--------------

[](#immutable-dtos)

A more realistic example using immutable DTOs for a blog system:

```
// config/dto.php
return Schema::create()
    ->dto(Dto::immutable('Article')->fields(
        Field::int('id')->required(),
        Field::string('title')->required(),
        Field::string('slug')->required(),
        Field::string('content'),
        Field::dto('author', 'Author')->required(),
        Field::collection('tags', 'Tag')->singular('tag'),
        Field::bool('published')->default(false),
        Field::string('publishedAt'),
    ))
    ->dto(Dto::immutable('Author')->fields(
        Field::string('name')->required(),
        Field::string('email'),
        Field::string('avatarUrl'),
    ))
    ->dto(Dto::immutable('Tag')->fields(
        Field::string('name')->required(),
        Field::string('slug')->required(),
    ))
    ->toArray();
```

```
// Creating from API/database response
$article = ArticleDto::createFromArray($apiResponse);
```

Reading in a template (e.g., Twig, Blade, or plain PHP):

```

        By

            on
