PHPackages                             anfischer/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. anfischer/dto

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

anfischer/dto
=============

A PHP implementation of the Data Transfer Object pattern (https://martinfowler.com/eaaCatalog/dataTransferObject.html)

v0.1.0(7y ago)19MITPHPPHP &gt;=7.1

Since Jun 25Pushed 7y ago1 watchersCompare

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

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

DTO - PHP Data Transfer Object
==============================

[](#dto---php-data-transfer-object)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a2a900b775776cbf3f023358bead439798f77a6cc5037c86d0a52c95e391e160/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616e666973636865722f64746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/anfischer/dto)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/c13325a236411cbcfec2088d3726ee04332f55f042a8c295e35baab0d14e2143/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f616e666973636865722f64746f2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/anfischer/dto)[![Coverage Status](https://camo.githubusercontent.com/e59099d2f83facb7d5463a0d5263b16157d3cea9d313f144c677d267a961edc9/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f616e666973636865722f64746f2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/anfischer/dto/code-structure)[![Quality Score](https://camo.githubusercontent.com/d4a64cb6b62cb803d05d21efb812c9c20b5ce6368198bc68fe3fef33766428be/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f616e666973636865722f64746f2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/anfischer/dto)[![Total Downloads](https://camo.githubusercontent.com/d67402062c27413dc6fb80863ddc4a9cf48745c74d0d61467817b348929564b8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616e666973636865722f64746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/anfischer/dto)

A PHP implementation of the Data Transfer Object pattern ().
The Data Transfer Object allows for public properties with forced validation of their data types.

This implementation supports all of PHP's eight primitive data types:

- String
- Integer
- Double *(or by PHP-implementation floating point numbers)*
- Boolean
- Array
- Object
- Null
- Resource

As well as forced boolean values of:

- True
    *and*
- False

Install
-------

[](#install)

Via Composer

```
$ composer require anfischer/dto
```

Usage
-----

[](#usage)

The DTO class can be used to generate generic Data Transfer Objects which does not enforce initializing type, but guaranties strict types for initialized properties (e.g. a property which is first initialized as string can not be changed to integer later)

```
use Anfischer\Dto\Dto;

class GenericDataTransferObject extends Dto
{
    protected $someProperty;
    protected $anotherProperty;
}

$dto = new GenericDataTransferObject;
$dto->someProperty = 1;
$dto->anotherProperty = null;

// ERROR - throws InvalidTypeException since type is changed from integer to string
$dto->someProperty = 'foo';

// OK - since it was first initialized as null
$dto->anotherProperty = 'foo';
```

The DTO class also allows for generating type hinted Data Transfer Objects.
When forcing types properties can not be initialized with other types than defined for the properties (e.g. a property which is defined as string can not be initialized as integer)

```
use Anfischer\Dto\Dto;

class TypeHintedDataTransferObject extends Dto
{
    protected $stringProperty;
    protected $integerProperty;

    public function getPropertyType($property): string
    {
        switch ($property) {
            case 'stringProperty':
                return 'string';
            case 'integerProperty':
                return 'integer';
        }
    }
}

$dto = new TypeHintedDataTransferObject;

$dto->stringProperty = 'foo';
$dto->integerProperty = 1;

// ERROR - throws InvalidTypeException since type has to be initialized as string
$dto->stringProperty = 1;

// ERROR - throws InvalidTypeException since type has to be initialized as integer
$dto->integerProperty = 'foo';
```

Finally, the DTO class allows for generating type hinted Data Transfer Objects with mixed types.

```
use Anfischer\Dto\Dto;

class TypeHintedDataTransferObject extends Dto
{
    protected $mixedProperty;

    public function getPropertyType($property): string
    {
        switch ($property) {
            case 'mixedProperty':
                return 'string|integer|array';
        }
    }
}

$dto = new MixedTypeHintedDataTransferObject;

$dto->mixedProperty = 'foo';
$dto->mixedProperty = 1;
$dto->mixedProperty = ['foo', 'bar', 'baz'];

// ERROR - throws InvalidTypeException since type has to be either string, integer or array
$dto->mixedProperty = 1.1;

// ERROR - throws InvalidTypeException since type has to be either string, integer or array
$dto->mixedProperty = false;
```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE\_OF\_CONDUCT](CODE_OF_CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Andreas Fischer](https://github.com/anfischer)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

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

Unknown

Total

1

Last Release

2876d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ede147c0254f2a0e0fb9833c0871b6e86f21f71c53b9c35b35fbdaa1a03bc4ec?d=identicon)[anfischer](/maintainers/anfischer)

---

Top Contributors

[![anfischer](https://avatars.githubusercontent.com/u/2504884?v=4)](https://github.com/anfischer "anfischer (8 commits)")

---

Tags

dtoanfischer

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[zero-to-prod/data-model

Transforms Data into Type-Safe DTOs.

14226.2k32](/packages/zero-to-prod-data-model)[dereuromark/cakephp-dto

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

2988.9k3](/packages/dereuromark-cakephp-dto)[nutgram/hydrator

Hydrator for PHP 8.0+

12265.2k6](/packages/nutgram-hydrator)[cerbero/dto

Data Transfer Object (DTO)

17119.4k1](/packages/cerbero-dto)

PHPackages © 2026

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