PHPackages                             diephp/data-object - 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. diephp/data-object

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

diephp/data-object
==================

Simple object to be used as a DTO/MODEL/ValueObject.

v1.0.0(1y ago)05MITPHPPHP ^7.4 || ^8.0

Since May 24Pushed 1y ago1 watchersCompare

[ Source](https://github.com/diephp/data-object)[ Packagist](https://packagist.org/packages/diephp/data-object)[ RSS](/packages/diephp-data-object/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

DataObject
==========

[](#dataobject)

Simple object to be used as a DTO/MODEL/ValueObject.

Overview
--------

[](#overview)

The `DataObject` class is a versatile data container designed for use as a Data Transfer Object (DTO), Model, or ValueObject. It can be initialized with an associative array and provides various methods for manipulating and accessing data. This class implements both the `\JsonSerializable` and `ArrayAccess` interfaces.

Features
--------

[](#features)

- Initialize with an associative array
- Implements `\JsonSerializable` and `ArrayAccess` interfaces
- Supports dot notation for nested data access
- Provides methods for merging, filtering, mapping, and transforming data
- Automatically converts objects to arrays if they implement a `toArray` method
- Various utility methods like `hash`, `flatten`, `collapse`, `clone`, etc.

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

[](#installation)

Install the package via Composer:

```
composer require diephp/dataobject
```

Or manually add it to your `composer.json`:

```
"require": {
    "diephp/dataobject": "^1.0.0"
}
```

Usage
-----

[](#usage)

### Initialization

[](#initialization)

Create a new instance of `DataObject`:

```
use DiePHP\DataObject;

$data = new DataObject([
    'name' => 'John Doe',
    'email' => 'john.doe@example.com'
]);
```

Or use the static `of` method:

```
$data = DataObject::of([
    'name' => 'John Doe',
    'email' => 'john.doe@example.com'
]);
```

### Accessing Data

[](#accessing-data)

Access data using property notation or array access:

```
echo $data->name; // John Doe
echo $data['email']; // john.doe@example.com
```

Check if a key exists:

```
if ($data->has('name')) {
    // Key exists
}
```

### Manipulating Data

[](#manipulating-data)

Set a value:

```
$data->set('name', 'Jane Doe');
```

Get a value with a default:

```
$email = $data->get('email', 'default@example.com');
```

Merge new data:

```
$data->merge([
    'address' => '123 Main St',
    'phone' => '123-456-7890'
]);
```

Filter data:

```
$data->filter(function ($value, $key) {
    return !empty($value);
});
```

Map data:

```
$mappedData = $data->map('*', function ($value) {
    return strtoupper($value);
});
```

### Utility Methods

[](#utility-methods)

Convert to array:

```
$array = $data->toArray();
```

Convert to JSON:

```
$json = json_encode($data);
```

Get the MD5 hash of the data:

```
$hash = $data->hash();
```

Clone the object:

```
$clone = $data->clone();
```

### Example

[](#example)

```
use DiePHP\DataObject;

$data = new DataObject([
    'user' => [
        'name' => 'John Doe',
        'email' => 'john.doe@example.com'
    ]
]);

echo $data->get('user.name'); // John Doe

$data->set('user.address', '123 Main St');
echo $data->get('user.address'); // 123 Main St

$data->merge(['user.phone' => '123-456-7890']);
echo $data->get('user.phone'); // 123-456-7890

$data->filter(function ($value, $key) {
    return !empty($value);
});

echo $data->hash(); // MD5 hash of the data

echo json_encode($data); // JSON representation
```

License
-------

[](#license)

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

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

725d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/843845c3857f227042e7ae4ea3f5e255f8e14fc59d6a971b72bc0670996e7701?d=identicon)[diephp](/maintainers/diephp)

---

Top Contributors

[![AnatolyAstapov](https://avatars.githubusercontent.com/u/12308843?v=4)](https://github.com/AnatolyAstapov "AnatolyAstapov (1 commits)")

---

Tags

dtodataobject

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/diephp-data-object/health.svg)

```
[![Health](https://phpackages.com/badges/diephp-data-object/health.svg)](https://phpackages.com/packages/diephp-data-object)
```

###  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)
