Skip to content

PHP DTOCode Generation for Data Transfer Objects

Framework-agnostic DTOs with zero runtime overhead, perfect IDE support, and TypeScript generation.

PHP DTO

Quick Example

Define your DTOs with the fluent PHP builder:

php
// config/dto.php
use PhpCollective\Dto\Config\{Dto, Field, Schema};

return Schema::create()
    ->dto(Dto::create('User')->fields(
        Field::int('id')->required(),
        Field::string('name')->required(),
        Field::string('email')->required(),
        Field::dto('address', 'Address'),
    ))
    ->dto(Dto::create('Address')->fields(
        Field::string('street'),
        Field::string('city'),
        Field::string('country'),
    ))
    ->toArray();

Generate and use:

bash
vendor/bin/dto generate
php
$user = UserDto::createFromArray($apiResponse);

$user->getName();           // string
$user->getAddress();        // AddressDto|null
$user->getAddressOrFail();  // AddressDto (throws if null)

Installation

bash
composer require php-collective/dto

Framework Integrations

Released under the MIT License.