PHPackages                             compositephp/entity - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. compositephp/entity

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

compositephp/entity
===================

PHP 8.1+ Smart entity with hydration

v0.1.16(5mo ago)8418↓66.7%1MITPHPPHP ^8.1CI passing

Since Oct 25Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/compositephp/entity)[ Packagist](https://packagist.org/packages/compositephp/entity)[ RSS](/packages/compositephp-entity/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (18)Used By (1)

Composite Entity
================

[](#composite-entity)

[![Latest Stable Version](https://camo.githubusercontent.com/b2b9c98b6aa6cacb182a92c46a106a1bebfae13c5acf0d9507ecad5c2314f513/68747470733a2f2f706f7365722e707567782e6f72672f636f6d706f736974657068702f656e746974792f762f737461626c65)](https://packagist.org/packages/compositephp/entity)[![Build Status](https://github.com/compositephp/entity/actions/workflows/main.yml/badge.svg)](https://github.com/compositephp/entity/actions)[![Codecov](https://camo.githubusercontent.com/470db1900c373a3fb5958c1fa875f6a75070c14b855f990643ee534ace1213d0/68747470733a2f2f636f6465636f762e696f2f67682f636f6d706f736974657068702f656e746974792f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/compositephp/entity/)

Composite Entity is a PHP 8.1+ lightweight class designed for efficient and intelligent data handling. It specializes in the serialization and deserialization of data, making it highly beneficial for database management.

Features
--------

[](#features)

- Converts database rows to strictly typed objects and vice versa.
- Streamlines database interactions.

Overview:

- [Requirements](#requirements)
- [Installation](#installation)
- [Quick example](#quick-example)
- [Advanced usage](#advanced-usage)

Requirements
------------

[](#requirements)

- PHP 8.1 or higher.

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

[](#installation)

Install using Composer::

```
$ composer require compositephp/entity
```

Supported column types:
-----------------------

[](#supported-column-types)

Composite Entity supports a wide range of data types:

- Basic types: String, Integer, Float, Bool, Array.
- Complex types: Object (stdClass), DateTime/DateTimeImmutable, Enum.
- Advanced types: Entity, Entity Lists or Maps, Collections (e.g., Doctrine Collection), Custom classes implementing Composite\\DB\\Entity\\CastableInterface.

Quick example
-------------

[](#quick-example)

```
use Composite\Entity\AbstractEntity;

class User extends AbstractEntity
{
    public function __construct(
        public readonly int $id,
        public string $email,
        public ?string $name = null,
        public bool $is_test = false,
        public array $languages = [],
        public Status $status = Status::ACTIVE,
        public readonly \DateTimeImmutable $created_at = new \DateTimeImmutable(),
    ) {}
}

enum Status
{
    case ACTIVE;
    case BLOCKED;
}
```

Example of serialization:

```
$user = new User(
    id: 123,
    email: 'john@example.com',
    name: 'John',
    is_test: false,
    languages: ['en', 'fr'],
    status: Status::BLOCKED,
);

var_export($user->toArray());

//will output
array (
  'id' => 123,
  'email' => 'user@example.com',
  'name' => 'John',
  'is_test' => false,
  'languages' => '["en","fr"]',
  'status' => 'BLOCKED',
  'created_at' => '2022-01-01 11:22:33.000000',
)
```

You can also deserialize (hydrate) entity from array:

```
$user = User::fromArray([
  'id' => 123,
  'email' => 'user@example.com',
  'name' => 'John',
  'is_test' => false,
  'languages' => '["en","fr"]',
  'status' => 'BLOCKED',
  'created_at' => '2022-01-01 11:22:33.000000',
]);
```

And that's it. No need for special getters or setters, no additional "behaviours" or extra layers of code. Composite Entity handles everything automatically, ensuring seamless data casting.

Advanced usage
--------------

[](#advanced-usage)

### Custom Hydration

[](#custom-hydration)

For tailored performance, implement your own hydrator:

1. Create a class implementing `Composite\Entity\HydratorInterface`.
2. Add `Composite\Entity\Attributes\Hydrator` attribute to your entity class.

### Useful Attributes

[](#useful-attributes)

- #### Composite\\Entity\\Attributes\\SkipSerialization

    [](#compositeentityattributesskipserialization)

    Exclude properties from hydration.
- #### Composite\\Entity\\Attributes\\ListOf

    [](#compositeentityattributeslistof)

    Define lists of entities within a property.

    Example:

    ```
    use Composite\Entity\AbstractEntity;
    use Composite\Entity\Attributes\ListOf;

    class User extends AbstractEntity
    {
        public readonly int $id;

        public function __construct(
            public string $name,
            #[ListOf(Setting::class)]
            public array $settings = [],
        ) {}
    }

    class Setting extends AbstractEntity
    {
        public function __construct(
            public readonly string $name,
            public bool $isActive,
        ) {}
    }
    ```

License:
--------

[](#license)

MIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information. Maintained by Composite PHP.

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance71

Regular maintenance activity

Popularity21

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

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

Recently: every ~177 days

Total

17

Last Release

166d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/585d414228f543dd8e3eb606f05a4b7bd52388a671abb52b70cc5bad4f7d1739?d=identicon)[compositephp](/maintainers/compositephp)

---

Top Contributors

[![compositephp](https://avatars.githubusercontent.com/u/38870693?v=4)](https://github.com/compositephp "compositephp (55 commits)")

---

Tags

datamapperentityhydrationphpphp81serialization

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/compositephp-entity/health.svg)

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

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M284](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)

PHPackages © 2026

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