PHPackages                             matephp/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. [API Development](/categories/api)
4. /
5. matephp/dto

ActiveLibrary[API Development](/categories/api)

matephp/dto
===========

MatePHP - DTO - Simple Data Transfer Objects (DTO) for any framework

1.3.3(5mo ago)044↓100%MITPHPPHP ^8.2

Since Mar 7Pushed 5mo agoCompare

[ Source](https://github.com/mate-php/dto)[ Packagist](https://packagist.org/packages/matephp/dto)[ RSS](/packages/matephp-dto/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (4)Versions (10)Used By (0)

Mate - DTO
==========

[](#mate---dto)

 Simple Data Transfer Objects (DTO) for any framework

[![Repo](https://camo.githubusercontent.com/9a90a3efeee26aed7d7f2feee9cd84566a26f9c362cc773b184d076210906e1c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6769746875622d677261793f6c6f676f3d676974687562)](https://packagist.org/packages/matephp/dto)[![Latest Stable Version](https://camo.githubusercontent.com/52c0775d2517308067777e6ece47fb55daa672be73e802941e5d12ebd118888e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6174657068702f64746f)](https://packagist.org/packages/matephp/dto)[![Unstable Version](https://camo.githubusercontent.com/6bb7c46af27d034e3c4de06b6b6720e33e4c35979a232bdbfd6b9c9dfbdd2b12/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f756e737461626c652d6465762d2d6d61696e2d6f72616e6765)](https://packagist.org/packages/matephp/dto)[![PHP from Packagist](https://camo.githubusercontent.com/b0c112ffd9c7aecf1cbd66809ae9d3ebf1dacdcbb47c4dfbf6554faebd695a9b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6d6174657068702f64746f2e7376673f6c6f676f3d706870)](https://packagist.org/packages/matephp/dto)[![Total Downloads](https://camo.githubusercontent.com/4ed360a08b846ddb8330a064031710691a116b76f368f8c9e1d6e8c2396a6996/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6174657068702f64746f)](https://packagist.org/packages/matephp/dto)[![License](https://camo.githubusercontent.com/16b61692b21d08ee5b57f5937c72c00ef44a58aed83056ebe698c34bdb239848/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d6174657068702f64746f)](https://packagist.org/packages/matephp/dto)[![Hits-of-Code](https://camo.githubusercontent.com/c56177ce617a1e622ae2d938a940248901f9e6d47da386cff7615f1dc1284e1a/68747470733a2f2f686974736f66636f64652e636f6d2f6769746875622f6d6174652d7068702f64746f3f6272616e63683d6d61696e)](https://hitsofcode.com/github/mate-php/dto/json?branch=main)

A DTO, or Data Transfer Object, is a design pattern used to transfer data between different layers or subsystems of an application. Its main function is to encapsulate data and minimize the number of calls to the database or other services.

Key features of DTOs:

- Simplicity: DTOs are simple objects that only contain data and no business logic.
- Encapsulation: DTOs encapsulate data, allowing you to control how information is accessed and modified.
- Efficiency: DTOs can improve performance by reducing the amount of data transferred between layers.
- Decoupling: DTOs help decouple different layers of an application, making code maintenance and evolution easier.

Some use cases:

- When you need to transfer data between different layers of an application, such as the presentation layer and the data layer.
- When you need to expose data through an API.
- When you need to optimize the performance of your application by reducing the amount of data being transferred.

Instalation
===========

[](#instalation)

**Via Composer**

```
composer require matephp/dto
```

Documentation
=============

[](#documentation)

Define DTO properties
---------------------

[](#define-dto-properties)

Properties can be set in the class definition or in the constructor ([Constructor Property Promotion](https://wiki.php.net/rfc/constructor_promotion))

```
use Mate\Dto\Dto;

class MyDto extends Dto
{
    public string $property1;
    public string $property2;
}

// or

class MyDto extends Dto
{
    public function __constuct(
        public string $property1,
        public string $property2
    ) {
    }
}
```

DTO instantation
----------------

[](#dto-instantation)

You can create DATA in different ways:

**Using `constructor`**

```
// ...

$dto = new MyDto(
    property1: 'value 1',
    property2: 'value 2',
);
```

**From array using `fromArray` static method**

```
// ...

$data = [
    "property1" => 'value 1',
    "property2" => 'value 2',
];

$dto = MyDto::fromArray($data);
```

**From object using `fromObject` static method**

```
// ...

$data = new stdClass();
$data->property1 = "value 1";
$data->property2 = "value 2";

$dto = MyDto::fromObject($data);
```

**From json string using `fromJson` static method**

```
// ...

$obj = new stdClass();
$obj->property1 = "value 1";
$obj->property2 = "value 2";

$data = json_encode($obj);

$dto = MyDto::fromJson($data);
```

**from other DTO instance using `fromDto` static method**

```
// ...

$otherDto = new MyDto(
    property1: 'value 1',
    property2: 'value 2',
);

$dto = MyDto::fromDto($otherDto);
```

Creating DTOs with dynamic properties (Flexible DTO)
----------------------------------------------------

[](#creating-dtos-with-dynamic-properties-flexible-dto)

To create a DTO that allows defining properties dynamically, the [attribute](https://www.php.net/manual/en/language.attributes.php) `#[Flexible]` is used

```
use Mate\Dto\Dto;
use Mate\Dto\Attributes\Flexible;

#[Flexible]
class MyFlexibleDTo extends Dto
{
    public function __constructor(
        public string $property1,
        public string $property2
    ) {
    }
}

$dto = new MyFlexibleDTo(
    property1: 'value 1',
    property2: 'value 2',
    property3: 'value 3'
);

echo $dto->property3; // print: value 3
```

When creating a DTO with dynamic properties but not setting the `#[Flexible]` attribute, an exception of type `Mate\Dto\Exceptions\NotFlexibleException` is thrown.

Access DTO data
---------------

[](#access-dto-data)

DTO data can be accessed as a property of an object or through array syntax

```
// ...

$dto = new MyDto(
    property1: 'value 1',
    property2: 'value 2',
);

echo $dto->property1; // print: value 1
echo $dto->property2; // print: value 2

echo $dto['property1']; // print: value 1
echo $dto['property2']; // print: value 2
```

**Uninitialized properties**

When an attribute is called that was not initialized, an `\Error` exception is thrown.

```
// ...

$dto = new MyDto(
    property1: 'value 1',
);

echo $dto->property2; // throw: \Error exception
```

**Properties with default value**

When a value is not sent to a property, the default value is used if it has one.

```
use Mate\Dto\Dto;

class MyDto extends Dto
{
    public function __constructor(
        public string $property1,
        public string $property2 = 'value 2'
    ) {
    }
}

$dtoA = new MyDto(
    property1: 'value 1',
);

$dtoB = new MyDto(
    property1: 'value 1',
    property2: 'other value'
);

echo $dtoA->property2; // print: value 2
echo $dtoA->property2; // print: other value
```

**Nullable Properties**

When a value is not sent to a nullable property it is automatically set to `null`.

```
use Mate\Dto\Dto;

class MyDto extends Dto
{
    public string $property1;
    public ?string $property2;
}

$dtoA = new MyDto(
    property1: 'value 1',
);

$dtoB = new MyDto(
    property1: 'value 1',
    property2: 'value 2';
);

echo $dtoA->property2; // print: null
echo $dtoB->property2; // print: value 2
```

**Ignored attributes**

You can ignore attributes so that they are not initialized and are not considered.

```
use Mate\Dto\Dto;
use Mate\Dto\Attributes\Ignored;

class MyDto extends Dto
{
    #[Ignored]
    public string $property1;
    public string $property2;
}

$dto = new MyDto(
    property1: 'value 1',
    property2: 'value 2',
);

echo $dto->property1; // throw: \Error exception
```

Transforming data
-----------------

[](#transforming-data)

You can convert your DTO to some formats:

**To array**

```
// ...

$dto = new MyDto(
    property1: 'value 1',
    property2: 'value 2',
);

echo print_r($dto->toArray, true);
// print:
// Array
// (
//     [property1] => value 1
//     [property2] => value 2
// )
```

If you do not need to transform the attributes of the DTO type into an array, you can set it to false.

```
use Mate\Dto\Dto;

class MyDtoWithoutNestedToArray extends Dto
{
    protected bool $nestedToArrayEnabled = false;
    // ...
}
```

**To database**

```
// ...

class Client extends Dto
{
    public int $id;
    public int $userId;
}

$dto = new MyDto(
    id: 1,
    userId: 2,
);

echo print_r($dto->toDatabase, true);
// print:
// Array
// (
//     [id] => 1
//     [user_id] => 2
// )
```

**To JSON string**

```
// ...

$dto = new MyDto(
    property1: 'value 1',
    property2: 'value 2',
);

echo print_r($dto->toJson, true);
// print: {"property1":"value 1","property2":"value 2"}
```

**To string**

```
// ...

$dto = new MyDto(
    property1: 'value 1',
    property2: 'value 2',
);

echo print_r((string) $dto, true);
// print: {"property1":"value 1","property2":"value 2"}
```

Testing
=======

[](#testing)

```
composer test

// or with coverage

composer test-coverage
```

QA Tools
========

[](#qa-tools)

```
composer phpcs
composer phpstan
composer phpmd
```

License
=======

[](#license)

MIT license. Please see the [license file](LICENSE) for more information.

###  Health Score

37

—

LowBetter than 84% of packages

Maintenance70

Regular maintenance activity

Popularity8

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity55

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 ~26 days

Recently: every ~47 days

Total

9

Last Release

170d ago

PHP version history (2 changes)1.0.0PHP ^8.3

1.3.0PHP ^8.2

### Community

Maintainers

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

---

Top Contributors

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

---

Tags

apimapperdata-transfer-objectrestfuldtodata model

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[phpexperts/simple-dto

A quick and easy DTO package.

521.1M7](/packages/phpexperts-simple-dto)[rekalogika/mapper

An object mapper for PHP and Symfony. Maps an object to another object. Primarily used for transforming an entity to a DTO and vice versa.

3846.0k1](/packages/rekalogika-mapper)[artyuum/request-dto-mapper-bundle

This bundle provides an easy way to automatically map the incoming request data to a DTO and optionally validate it.

515.8k](/packages/artyuum-request-dto-mapper-bundle)[teepluss/api

Laravel 4 Internal Request (HMVC)

7034.0k](/packages/teepluss-api)[orisai/object-mapper

Raw data mapping to validated objects

1133.5k2](/packages/orisai-object-mapper)

PHPackages © 2026

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