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

ActiveLibrary

xp-lang/xp-records
==================

XP recods for PHP

v3.1.0(1y ago)11.4k↑500%BSD-3-ClausePHPPHP &gt;=7.4.0

Since Mar 28Pushed 1y ago2 watchersCompare

[ Source](https://github.com/xp-lang/xp-records)[ Packagist](https://packagist.org/packages/xp-lang/xp-records)[ Docs](http://xp-framework.net/)[ RSS](/packages/xp-lang-xp-records/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (20)Used By (0)

XP records for PHP
==================

[](#xp-records-for-php)

[![Build status on GitHub](https://github.com/xp-lang/xp-records/workflows/Tests/badge.svg)](https://github.com/xp-lang/xp-records/actions)[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)[![Requires PHP 7.4+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_4plus.svg)](http://php.net/)[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)[![Latest Stable Version](https://camo.githubusercontent.com/925740634c4fe2ff5e8bebb055d5799c1378f2f4fcc277236d23a9c497bbf379/68747470733a2f2f706f7365722e707567782e6f72672f78702d6c616e672f78702d7265636f7264732f76657273696f6e2e737667)](https://packagist.org/packages/xp-lang/xp-records)

Plugin for the [XP Compiler](https://github.com/xp-framework/compiler/) which adds a `record` syntax to the PHP language. Records declare a final class with immutable components for each of its members and appropriate accessors and a constructor, which implements the `lang.Value` interface.

Example
-------

[](#example)

```
// Declaration
namespace com\example;

use IteratorAggregate, Traversable;

record Range(int $lo, int $hi) implements IteratorAggregate {
  public function getIterator(): Traversable {
    for ($i= $this->lo; $i hi; $i++) {
      yield $i;
    }
  }
}

// Usage
$r= new Range(1, 10);
$r->lo();       // 1
$r->hi();       // 10
$r->toString(); // "com.example.Range(lo: 1, hi: 10)"

foreach ($r as $item) {
  // 1, 2, 3, ... 10
}
```

*Note: The generated `toString()`, `hashCode()` and `compareTo()` methods may be overriden by supplying an implementation in the record body.*

Initialization
--------------

[](#initialization)

To verify constructor parameters, add an initialization block as follows:

```
use lang\IllegalArgumentException;

record Range(int $lo, int $hi) {
  init {
    if ($this->lo > $this->hi) {
      throw new IllegalArgumentException('Lower border may not exceed upper border');
    }
  }
}
```

This block is called *after* the members have been initialized from the constructor parameters.

Destructuring
-------------

[](#destructuring)

To destructure a record into its members, use the invocation syntax:

```
// Using the declaration from above:
$r= new Range(1, 10);

// Use https://wiki.php.net/rfc/short_list_syntax (>= PHP 7.1)
[$lo, $hi]= $r();

// Optionally map the members, returns the string "1..10"
$string= $r(fn($lo, $hi) => "{$lo}..{$hi}");
```

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

[](#installation)

After installing the XP Compiler into your project, also include this plugin.

```
$ composer require xp-framework/compiler
# ...

$ composer require xp-lang/xp-records
# ...
```

No further action is required.

See also
--------

[](#see-also)

- [Kotlin Data Classes](https://kotlinlang.org/docs/reference/data-classes.html)
- [Java 14 Records](https://docs.oracle.com/en/java/javase/14/language/records.html)
- [Java 14 Feature Spotlight: Records](https://www.infoq.com/articles/java-14-feature-spotlight/)
- [C# structs](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct)
- [C# records](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record)

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

Established project with proven stability

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

Recently: every ~236 days

Total

19

Last Release

609d ago

Major Versions

v0.4.0 → v1.0.02020-03-28

v1.2.1 → v2.0.02022-01-24

v2.5.0 → v3.0.02024-03-24

PHP version history (2 changes)v0.1.0PHP &gt;=7.0.0

v3.0.0PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/07d18d882c8b4aaf3466432f64018214f2771eda333202175431ee7233795376?d=identicon)[thekid](/maintainers/thekid)

---

Top Contributors

[![thekid](https://avatars.githubusercontent.com/u/696742?v=4)](https://github.com/thekid "thekid (82 commits)")

---

Tags

compiler-pluginphp7php8recordxp-compilerxp-frameworklanguagemodulexp

### Embed Badge

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

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

PHPackages © 2026

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