PHPackages                             hoa/option - 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. hoa/option

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

hoa/option
==========

The Hoa\\Option library.

1.17.08.29(8y ago)102.5k3[2 issues](https://github.com/hoaproject/Option/issues)[1 PRs](https://github.com/hoaproject/Option/pulls)BSD-3-ClausePHPPHP &gt;=7.1

Since Aug 29Pushed 5y ago6 watchersCompare

[ Source](https://github.com/hoaproject/Option)[ Packagist](https://packagist.org/packages/hoa/option)[ Docs](https://hoa-project.net/)[ RSS](/packages/hoa-option/feed)WikiDiscussions master Synced 2d ago

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

 [![Hoa](https://camo.githubusercontent.com/2b5c32c5d4bc5e9298821b22d364a522e2dbc0295c1c011b1f9f86a4d07df07e/68747470733a2f2f7374617469632e686f612d70726f6a6563742e6e65742f496d6167652f486f612e737667)](https://camo.githubusercontent.com/2b5c32c5d4bc5e9298821b22d364a522e2dbc0295c1c011b1f9f86a4d07df07e/68747470733a2f2f7374617469632e686f612d70726f6a6563742e6e65742f496d6167652f486f612e737667)

---

 [![Build status](https://camo.githubusercontent.com/fb6ee18e337c3a37fd4365d84017c01f6921a4a2a5bbd8b3a3f4e319aff73326/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f686f6170726f6a6563742f4f7074696f6e2f6d61737465722e737667)](https://travis-ci.org/hoaproject/Option) [![Code coverage](https://camo.githubusercontent.com/1142fe8f835e8a4a87947e931a5b7df5e4ed0a44c6b8840b024a4a4eef34ef3a/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f686f6170726f6a6563742f4f7074696f6e2f6d61737465722e737667)](https://coveralls.io/github/hoaproject/Option?branch=master) [![Packagist](https://camo.githubusercontent.com/4bbc3a57972e03d70963d6eb95838998057a98edbbe10dfdd79b7cb1dc8ab7e8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686f612f6f7074696f6e2e737667)](https://packagist.org/packages/hoa/option) [![License](https://camo.githubusercontent.com/68f2451958ba45bed9313da7cacd9ac8d7356b820eb3df6fd0b8b9723c6d3fde/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f686f612f6f7074696f6e2e737667)](https://hoa-project.net/LICENSE)

 Hoa is a **modular**, **extensible** and **structured** set of PHP libraries.
 Moreover, Hoa aims at being a bridge between industrial and research worlds.

Hoa\\Option
===========

[](#hoaoption)

[![Help on IRC](https://camo.githubusercontent.com/4dbc9c9d28c30cf1ab591f4bb8212fe4dbddc734145df532a9bb86b09878d4c6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f68656c702d253233686f6170726f6a6563742d6666303036362e737667)](https://webchat.freenode.net/?channels=#hoaproject)[![Help on Gitter](https://camo.githubusercontent.com/8c4c85951788ff606b1268cb3dd946be05e3054795455d0a7b9250711bc2ac05/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f68656c702d6769747465722d6666303036362e737667)](https://gitter.im/hoaproject/central)[![Documentation](https://camo.githubusercontent.com/7059ad5f1a363f9098686c59d432f01d7330aed9d4b6c8111d985fd64cfc6c60/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f63756d656e746174696f6e2d6861636b5f626f6f6b2d6666303036362e737667)](https://central.hoa-project.net/Documentation/Library/Option)[![Board](https://camo.githubusercontent.com/fd81654ba14b3aca3a713e1b471bc3fc3ba7b5bb3761ccffd6eea2e2ed1fa5ca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6f7267616e69736174696f6e2d626f6172642d6666303036362e737667)](https://waffle.io/hoaproject/option)

This library is an implementation of the famous [`Option` polymorphic type](https://en.wikipedia.org/wiki/Option_type)(also called `Maybe`). An `Option` represents an optional value, either there is `Some` value, or there is `None` which is the equivalent of `null`. This is a convenient and safe way to manipulate an optional value.

[Learn more](https://central.hoa-project.net/Documentation/Library/Option).

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

[](#installation)

With [Composer](https://getcomposer.org/), to include this library into your dependencies, you need to require [`hoa/option`](https://packagist.org/packages/hoa/option):

```
$ composer require hoa/option '~1.0'
```

For more installation procedures, please read [the Source page](https://hoa-project.net/Source.html).

Testing
-------

[](#testing)

Before running the test suites, the development dependencies must be installed:

```
$ composer install
```

Then, to run all the test suites:

```
$ vendor/bin/hoa test:run
```

For more information, please read the [contributor guide](https://hoa-project.net/Literature/Contributor/Guide.html).

Quick usage
-----------

[](#quick-usage)

The following examples illustrate how to use the `Hoa\Option\Option` class.

### Build an optional value

[](#build-an-optional-value)

There are two static methods to allocate an optional value:

- `Hoa\Option\Option::some($value)` for some value,
- `Hoa\Option\Option::none()` for no value.

Two functional aliases exist, respectively:

- `Hoa\Option\Some($value)`, and
- `Hoa\Option\None()`.

In the next examples, it is assumed that `use function Hoa\Option\{Some, None}` is declared, so that we can use `Some` and `None` directly.

### Basic operations

[](#basic-operations)

The `isSome` and `isNone` methods respectively return `true` if the option contains some value or none:

```
$x = Some(42);
$y = None();

assert($x->isSome());
assert($y->isNone());
```

The `unwrap` method returns the contained value if there is some, or throws a `RuntimeException` if there is none:

```
assert(Some(42)->unwrap() === 42);
assert(None()->unwrap()); // will throw a `RuntimeException`.
```

In general, because of the unexpected exception, its use is discouraged. Prefer to use either: `expect`, `unwrapOr`, `isSome`, or `isNone` for instance.

One common mistake is to think that it is required to extract/unwrap the value from the option. Actually, the `Hoa\Option\Option` API is designed to always manipulate an option without having the need to extract its contained value. New options can be constructed or mapped, see next sections.

The `unwrap` method can throw a `RuntimeException` with a default message. To have a custom message, please use the `expect` method:

```
$x = None();

assert($x->expect('Damn…') === 42);
```

The `unwrapOr` method returns the contained value if there is some, or a default value if none:

```
$x = Some(42);
$y = None();

assert($x->unwrapOr(153) === 42);
assert($y->unwrapOr(153) === 153);
```

The `unwrapOrElse` method returns the contained value if there is some, or computes a default value from the given callable if none:

```
$x = Some(42);
$y = None();

$else = function (): int { return 153; };

assert($x->unwrapOrElse($else) === 42);
assert($y->unwrapOrElse($else) === 153);
```

### Mappers

[](#mappers)

Mappers transform an option into another option by applying a callable to the contained value if some.

```
$x = Some('Hello, World!');
$y = None();

assert($x->map('strlen') == Some(13));
assert($x->map('strlen') == None());
```

The `mapOr` mapper transform an option into another option but use a default value if there is no contained value:

```
$x = None();

assert($x->mapOr('strlen', 0) == Some(0));
```

The result of `mapOr` is always an option with some value.

The `mapOrElse` mapper is similar to `mapOr` but computes the default value from a callable:

```
$x    = None();
$else = function (): int { return 0; };

assert($x->mapOrElse('strlen', $else) == Some(0));
```

### Boolean operations

[](#boolean-operations)

It is possible to apply boolean operations on options. The `and`method returns a none option if the current option has no value, otherwise it returns the right option:

```
assert(Some(42)->and(Some(153)) == Some(153));
assert(Some(42)->and(None())    == None());
assert(None()->and(Some(153))   == None());
```

The `andThen` method returns a none option if the current option has no value, otherwise it returns a new option computed by a callable. Some languages call this operation `flatmap`.

```
$square = function (int $x): Option {
    return Some($x * $x);
};
$nop = function (): Option {
    return None();
};

assert(Some(2)->andThen($square)->andThen($square) == Some(16));
assert(Some(2)->andThen($nop)->andThen($square)    == None());
```

The `or` method returns the current option if it has some value, otherwise it returns the right option:

```
assert(Some(42)->or(Some(153)) == Some(42));
assert(None()->or(Some(153))   == Some(153));
```

Finally the `orElse` option returns the option if it has some value, otherwise it returns a new option computed by a callable:

```
$somebody = function (): Option {
    return Some('somebody');
};

assert(None()->orElse($somebody) == Some('somebody'));
```

Documentation
-------------

[](#documentation)

The [hack book of `Hoa\Option`](https://central.hoa-project.net/Documentation/Library/Option)contains detailed information about how to use this library and how it works.

To generate the documentation locally, execute the following commands:

```
$ composer require --dev hoa/devtools
$ vendor/bin/hoa devtools:documentation --open
```

More documentation can be found on the project's website: [hoa-project.net](https://hoa-project.net/).

Getting help
------------

[](#getting-help)

There are mainly two ways to get help:

- On the [`#hoaproject`](https://webchat.freenode.net/?channels=#hoaproject)IRC channel,
- On the forum at [users.hoa-project.net](https://users.hoa-project.net).

Contribution
------------

[](#contribution)

Do you want to contribute? Thanks! A detailed [contributor guide](https://hoa-project.net/Literature/Contributor/Guide.html) explains everything you need to know.

License
-------

[](#license)

Hoa is under the New BSD License (BSD-3-Clause). Please, see [`LICENSE`](https://hoa-project.net/LICENSE) for details.

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

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

3228d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a410060743cec0e0d8654661b726a7a25f5888f18059d4c942a8fd9a8128f0da?d=identicon)[Hoa](/maintainers/Hoa)

---

Top Contributors

[![Hywan](https://avatars.githubusercontent.com/u/946104?v=4)](https://github.com/Hywan "Hywan (23 commits)")[![mathroc](https://avatars.githubusercontent.com/u/291531?v=4)](https://github.com/mathroc "mathroc (1 commits)")

---

Tags

hoalibrarymaybeoptionphplibraryoptionmaybe

### Embed Badge

![Health badge](/badges/hoa-option/health.svg)

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

PHPackages © 2026

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