PHPackages                             phpgears/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. phpgears/dto

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

phpgears/dto
============

General purpose immutable Data Transfer Objects for PHP

0.3.1(6y ago)25.2k2MITPHPPHP ^7.1CI failing

Since Sep 16Pushed 5y ago3 watchersCompare

[ Source](https://github.com/phpgears/dto)[ Packagist](https://packagist.org/packages/phpgears/dto)[ Docs](https://github.com/phpgears/dto)[ RSS](/packages/phpgears-dto/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)Dependencies (16)Versions (8)Used By (2)

[![PHP version](https://camo.githubusercontent.com/d0b5687c6812c5d52d86a548e09db527eeb7860f82adbb677de00a36ddbed1b4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253345253344372e312d3838393242462e7376673f7374796c653d666c61742d737175617265)](http://php.net)[![Latest Version](https://camo.githubusercontent.com/c540d0408a5a0d87f35cb7b2710f099696c305e29626c13165b3340fd71569e5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70687067656172732f64746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/phpgears/dto)[![License](https://camo.githubusercontent.com/6a48d765d51e9627882ff6a712247326b0158bfeaaf25d54b72a2dc20febe63d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f70687067656172732f64746f2e7376673f7374796c653d666c61742d737175617265)](https://github.com/phpgears/dto/blob/master/LICENSE)

[![Build Status](https://camo.githubusercontent.com/20275e24352b897c630af06c0a0b60f372f03e3921003a817e76d737140ed3a7/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6d2f70687067656172732f64746f2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.com/github/phpgears/dto)[![Style Check](https://camo.githubusercontent.com/ac16f5107219ab424bd5883b50fc212acddecdee262a18bf2d767d6b70a4a26e/68747470733a2f2f7374796c6563692e696f2f7265706f732f3134383834303936312f736869656c64)](https://styleci.io/repos/148840961)[![Code Quality](https://camo.githubusercontent.com/318dd54e8d048b7517a5e6933fa1b5c2465b1d2d211f434bd43707332aaff57a/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f70687067656172732f64746f2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/phpgears/dto)[![Code Coverage](https://camo.githubusercontent.com/2b4302fcef22e46f4e130abefe92d16466414bd5358e60432b40c759994a2558/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f70687067656172732f64746f2e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/github/phpgears/dto)

[![Total Downloads](https://camo.githubusercontent.com/130a3e53e39f2dcc1115d33e7dd14f0083b4536330ec662e9da7b0c1f77477a9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70687067656172732f64746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/phpgears/dto/stats)[![Monthly Downloads](https://camo.githubusercontent.com/fc941b09a88716310aef89d3c6e34a10a8c5c768e1ba3b02f5f6ba00ad6a1f44/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f70687067656172732f64746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/phpgears/dto/stats)

DTO
===

[](#dto)

General purpose immutable Data Transfer Objects for PHP

This library provides a means to implement DTO classes as long as three different implementations of general purpose abstract DTO objects you can extend from

This DTO objects are immutable as can be thanks to [gears/immutability](https://github.com/phpgears/immutability) that means once the DTO is created there is no way a value on it is mutated (inside PHP boundaries)

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

[](#installation)

### Composer

[](#composer)

```
composer require phpgears/dto

```

Usage
-----

[](#usage)

Require composer autoload file

```
require './vendor/autoload.php';
```

You can use ImmutabilityBehaviour and PayloadBehaviour in any object you want to have immutable DTO functionality

```
use Gears\Immutability\ImmutabilityBehaviour;
use Gears\DTO\DTO;
use Gears\DTO\PayloadBehaviour;

class MyDTO implements DTO, MyDTOInterface
{
    use ImmutabilityBehaviour, PayloadBehaviour {
        PayloadBehaviour::__call insteadof ImmutabilityBehaviour;
    }

    public function __construct(array $parameters)
    {
        $this->assertImmutable();

        $this->setPayload($parameters);
    }

    final public function getAllowedInterfaces(): array
    {
        return [DTO::class, MyDTOInterface::class];
    }
}
```

If you just need a plain DTO object it gets a lot easier, this boilerplate code is already in place for you by extending `Gears\DTO\AbstractDTO` or `Gears\DTO\AbstractScalarDTO` classes

Protected constructors force you to create "named constructors", this has a very useful side effect, you get to type-hint all your DTO parameters

```
use Gears\DTO\AbstractScalarDTO;

/**
 * @method hasName(): bool
 * @method getName(): string
 * @method hasLastName(): bool
 * @method getLastName(): string
 * @method hasDate(): bool
 * @method getDate(): \DateTimeImmutable
 */
class MyDTO extends AbstractScalarDTO
{
    /**
     * Custom named constructor.
     *
     * @param string $name
     * @param string $lastName
     * @param DateTimeImmutable $date
     *
     * @return self
     */
    public static function instantiate(
        string $name,
        string $lastName,
        \DateTimeImmutable $date
    ): self {
        return new static([
            'name' => $name,
            'lastName' => $lastName,
            'date' => $date->setTimezone(new \DateTimeZone('UTC'))->format('U'),
        ]);
    }

    /**
     * Transforms 'date' parameter every time it is accessed.
     */
    protected function outputDate(string $date): \DateTimeImmutable
    {
        return DateTimeImmutable::createFromFormat('U', $date);
    }
}
```

The difference between `Gears\DTO\AbstractDTO` and `Gears\DTO\AbstractScalarDTO` is that the later ensures all payload is either a scalar value (null, string, int, float or bool) or an array of scalar values. Its purpose is to ensure the object can be securely serialized, it is the perfect match to create Domain Events, or CQRS Commands/Queries

Finally `Gears\DTO\AbstractDTOCollection` is a special type of DTO that only accepts a list of elements, being these elements implementations of DTO interface itself. This object is meant to be used as a return value when several DTOs should be returned, for example from a DDBB query result

You can take advantage of magic method \_\_call on DTO objects to access parameters. If you plan to use this feature it's best to annotate this magic accessors at class level with `@method` phpDoc tag, this will help your IDE auto-completion

Contributing
------------

[](#contributing)

Found a bug or have a feature request? [Please open a new issue](https://github.com/phpgears/dto/issues). Have a look at existing issues before.

See file [CONTRIBUTING.md](https://github.com/phpgears/dto/blob/master/CONTRIBUTING.md)

License
-------

[](#license)

See file [LICENSE](https://github.com/phpgears/dto/blob/master/LICENSE) included with the source code for a copy of the license terms.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~107 days

Recently: every ~133 days

Total

6

Last Release

2260d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4c50421f1ab4148354dc2dd5dcaba168656b17ea913b310d112deb39a6f73ca1?d=identicon)[juliangut](/maintainers/juliangut)

---

Top Contributors

[![juliangut](https://avatars.githubusercontent.com/u/1104131?v=4)](https://github.com/juliangut "juliangut (16 commits)")

---

Tags

dtoimmutable

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phpgears-dto/health.svg)

```
[![Health](https://phpackages.com/badges/phpgears-dto/health.svg)](https://phpackages.com/packages/phpgears-dto)
```

###  Alternatives

[aeon-php/calendar

PHP type safe, immutable calendar library

2079.7M16](/packages/aeon-php-calendar)[qaribou/immutable.php

Immutable, highly-performant collections, well-suited for functional programming and memory-intensive applications.

344146.0k](/packages/qaribou-immutablephp)[innmind/immutable

Immutable PHP primitive wrappers

75218.0k74](/packages/innmind-immutable)[zero-to-prod/data-model

Transforms Data into Type-Safe DTOs.

14226.2k32](/packages/zero-to-prod-data-model)[rtlopez/decimal

An object oriented immutable arbitrary-precision arithmetic library for PHP

27262.8k2](/packages/rtlopez-decimal)[dereuromark/cakephp-dto

A CakePHP plugin for generating immutable Data Transfer Objects with full type safety

2988.9k3](/packages/dereuromark-cakephp-dto)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
