PHPackages                             baethon/union - 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. baethon/union

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

baethon/union
=============

Union type for PHP

v1.0.0(7y ago)251MITPHPPHP &gt;=7.1CI failing

Since Aug 17Pushed 5y ago2 watchersCompare

[ Source](https://github.com/baethon/union.php)[ Packagist](https://packagist.org/packages/baethon/union)[ RSS](/packages/baethon-union/feed)WikiDiscussions master Synced yesterday

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

baethon/union
=============

[](#baethonunion)

Provides utilities to define tagged unions.

Tagged unions?
==============

[](#tagged-unions)

I'm not good with words, yet folks from [Folktale](https://folktale.origamitower.com/api/v2.1.0/en/folktale.adt.union.html) describe it nicely:

> Modelling data is important for a range of reasons. From performance to correctness to safety. Tagged unions give you a way of modelling choices that forces the correct handling of them, unlike predicate-based branching, such as the one used by if statements and other common control flow structures.

Installation
============

[](#installation)

```
composer require baethon/union
```

Usage
=====

[](#usage)

To create a tag union it's required to create a class which extends `Baethon\Union\AbstractUnion`.

```
class Maybe extends \Baethon\Union\AbstractUnion
{
}
```

Also you need to define tags which will represent state of the union.

```
class Maybe extends \Baethon\Union\AbstractUnion
{
	const SOME = 'Some:x';

	const NONE = 'None';
}
```

Signatures
----------

[](#signatures)

Each tag has a definition (called *signature*), which can be defined using following syntax:

```
{Name}[:[param1[, param2[, ...[, paramN]]]]]

```

Parameters define whether a tag will hold any value(s) (called *arguments*). They're optional.

Working with unions
-------------------

[](#working-with-unions)

Union can be invoked using static constructor:

```
$some = Maybe::Some(1);
```

To work with the state of the union you should use `matchWith()`. It will return the value returned by the matching callback:

```
function addTen(Maybe $maybe) {
	return $maybe->matchWith([
		'Some' => function ($x) {
			return $x + 10;
		},
		'None' => function () {
			throw new \Exception('Sorry, can\'t add a number to nothing');
		}
	]);
}

addTen($some); // 11
```

`matchWith()` will check if all possible branches are mapped:

- if a tag is not covered by map `UnderflowException` will be thrown
- if map covers more tags than defined ones it will throw 'OverflowException'.

It's possible to use wildcard map to cover all other cases:

```
$some->matchWith([
	'*' => function () {
		return 100;
	}
]); // 100
```

You can use defined const values in mapping:

```
$some->matchWith([
	Maybe::SOME => function () {},
	Maybe::NONE => function () {}
]);
```

Testing
=======

[](#testing)

```
./vendor/bin/phpunit

```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% 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

2874d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/488bdbbb3187d1f4f6d9c92f0c716ad413f5e7fe53d3ec5ffefb37cfcad4ecf6?d=identicon)[radmen](/maintainers/radmen)

---

Top Contributors

[![radmen](https://avatars.githubusercontent.com/u/1190255?v=4)](https://github.com/radmen "radmen (2 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/baethon-union/health.svg)

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

###  Alternatives

[woothee/woothee

Cross-language UserAgent classifier library, PHP implementation

107643.0k4](/packages/woothee-woothee)[overnaive/dingtalkrobot

DingTalk Robot

111.7k](/packages/overnaive-dingtalkrobot)

PHPackages © 2026

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