PHPackages                             withinboredom/records - 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. withinboredom/records

ActiveLibrary

withinboredom/records
=====================

Records in PHP

v0.1.3(9mo ago)51482MITPHP

Since Jun 8Pushed 9mo ago1 watchersCompare

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

READMEChangelog (4)Dependencies (1)Versions (5)Used By (2)

PHP Records
===========

[](#php-records)

This library provides a `Record` base class to build records with.

Install with

```
composer require withinboredom/records

```

Examples
--------

[](#examples)

The below example shows how to create a User class that contains several properties

```
use Withinboredom\Record;
use Withinboredom\Record\Attributes\ConstrainWith;

readonly class User extends Record {
	public string $name;
	public string $email;

	#[ConstrainWith(changeTogether: 'email')]
	#[ConstrainWith(changeTogether: 'name')]
	public int $id;

	public static function from(string $name, int $id, string $email): static {
		return self::fromArgs(name: $name, id: $id, email: $email);
	}
}
```

All you need to do is define a factory constructor, and the base class will handle the rest. Using it is relatively simple:

```
$user1 = User::from('Rob', 12, 'rob@example.com');
// later
$user2 = User::from('Rob', 12, 'rob@example.com');

assert($user1 === $user2); // true

$bob = $user1->with(name: 'Bob');

assert($user1 !== $bob); // true

$other = $bob->with(id: 42); // throws a LogicException
```

Attributes
----------

[](#attributes)

This library comes with a couple of attributes:

### Immutable

[](#immutable)

Prevents accidentally mutating a property with `with()`. This is useful when you are manually setting the identity and want to ensure the identity remains stable.

```
class Currency extends Record {
  public int $pennies;

  #[Immutable]
  public string $code;

  public function from(string $code, int $pennies): self {
    return self::fromArgs(code: $code, pennies: $pennies);
  }
}
```

This will prevent you from accidentally changing the code by doing `$balance->with(code: 'EUR')`.

### ConstrainWith

[](#constrainwith)

Requires a property changed with `with()` to also require other properties to be changed. Note that when we say "change" here, we do not mean the value, but it must be included together.

```
class Currency extends Record {
  public int $pennies;

  #[ConstrainWith('pennies')]
  public string $code;

  public function from(string $code, int $pennies): self {
    return self::fromArgs(code: $code, pennies: $pennies);
  }
}

$balance = Currency::from('USD', 42);
$transfer = $balance->with(code: 'EUR', pennies: 42); // constrain just requires it to be in the with(), not the value to change
```

Advanced usages
---------------

[](#advanced-usages)

There are several hooks you can use to either increase the performance or behaviour:

> `protected static function deriveIdentity(mixed ...$args): object|int|string|array`

Overriding this method allows you to determine how an object’s identity is derived. The default implementation assumes you are creating a value object where the identity is the properties in the object. This may not always be the case, so this allows you to skip fields and generate a different equality.

> `protected static function create(mixed ...$args): static`

Overriding this method allows you to change how objects are created when they do not already exist in the interned map. It is not recommended to change this.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance56

Moderate activity, may be stable

Popularity16

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity30

Early-stage or recently created project

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

Total

4

Last Release

289d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8f5995953815a492e3bf6276044042d84035247139141ec7fd013c13af4e6dd4?d=identicon)[withinboredom](/maintainers/withinboredom)

---

Top Contributors

[![withinboredom](https://avatars.githubusercontent.com/u/1883296?v=4)](https://github.com/withinboredom "withinboredom (18 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/withinboredom-records/health.svg)

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

PHPackages © 2026

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