PHPackages                             phunkie/phunkie-adt - 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. phunkie/phunkie-adt

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

phunkie/phunkie-adt
===================

0.1.0(8y ago)11491PHP

Since Aug 20Pushed 8y ago1 watchersCompare

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

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

Phunkie ADTs: Algebraic Data Types for PHP
==========================================

[](#phunkie-adts-algebraic-data-types-for-php)

"In computer programming, especially functional programming and type theory, an algebraic data type is a kind of composite type, i.e., a type formed by combining other types." — good old Wikipedia

Sum Types
---------

[](#sum-types)

In computer science sum type, is a data structure used to hold a value that could take on several different, but fixed, types. Only one of the types can be in use at any one time, and a tag field explicitly indicates which one is in use.

Let's say we want to create a type Weekday. We want it to be limited to the possible 7 days in the week: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday.

We could do that in a number of ways using typical imperative or Object Oriented techniques. We could use enums, constants grouped under an interface or inheritance.

At the moment PHP does not have enums, so that one is not a choice.

Grouping constants in a interface is a popular alternative, but it does not give any type safety.

```
