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

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

jjware/option
=============

PHP Option sum type

1.1.0(7y ago)016MITPHPPHP &gt;=7.0.0

Since Apr 29Pushed 7y agoCompare

[ Source](https://github.com/jjware/php-option)[ Packagist](https://packagist.org/packages/jjware/option)[ RSS](/packages/jjware-option/feed)WikiDiscussions master Synced 3d ago

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

Option
======

[](#option)

[![Build Status](https://camo.githubusercontent.com/5f860cb7b258899271cecfc6dc9fc8cd10dd62d881e79d99bdc9a267bf2430bc/68747470733a2f2f7472617669732d63692e6f72672f6a6a776172652f7068702d6f7074696f6e2e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/5f860cb7b258899271cecfc6dc9fc8cd10dd62d881e79d99bdc9a267bf2430bc/68747470733a2f2f7472617669732d63692e6f72672f6a6a776172652f7068702d6f7074696f6e2e7376673f6272616e63683d6d6173746572)

Option sum type for PHP

Getting Started
---------------

[](#getting-started)

```
composer require jjware/option

```

Creation
--------

[](#creation)

The `Option` class resides in namespace `JJWare\Utils\Option`

You can create an `Option` simply by calling the static `some` method:

```
$opt = Option::some('example value');
```

If you have a variable that may contain a `null` value, you may use the `nullable` static method:

```
$opt = Option::nullable($value);
```

If you have a case where you need to return an empty value, you may use the `none` static method:

```
$opt = Option::none();
```

Usage
-----

[](#usage)

Once you have an `Option`, there are many operations you can perform against it.

Let's say we have a function that may or may not return a value:

```
function getSetting(string $setting) : Option
{
    // Try to find the setting if it exists...
    return Option::nullable($result);
}
```

You may provide a default value in the case that your `Option` is empty:

```
$port = getSetting('port')->getOrElse(8080);
```

If your default value requires expensive calculation or calls to external resources, you may only want to get the default value when necessary:

```
$port = getSetting('port')->getOrElseGet(function () use ($db) {
    return $db->getDefaultPortFromDatabase();
});

// or using an instance method reference

$port = getSetting('port')->getOrElseGet([$db, 'getDefaultPortFromDatabase']);
```

The absence of a value may be an exceptional case for you:

```
$port = getSetting('port')->getOrThrow(function () {
   return new UnderflowException("setting does not exist");
});
```

You may need to change the value within the `Option` in some way if it exists:

```
$port = getSetting('port')->map(function ($x) {
   return intval($x);
})->getOrElse(8080);

// or using a function reference

$port = getSetting('port')->map('intval')->getOrElse(8080);
```

You may have a need to map to an entirely different `Option`:

```
$scheme = getSetting('port')->flatMap(function ($x) {
   return getSchemeForPort($x);
})->getOrElse('http');

// or as a function reference

$scheme = getSetting('port')->flatMap('getSchemeForPort')->getOrElse('http');
```

You may not want the value unless it meets specific criteria:

```
$port = getSetting('port')->filter(function ($x) {
    return $x >= 1024 && $x getOrElse(8080);

// or using a static method reference

$port = getSetting('port')->filter('Filters::registeredPort')->getOrElse(8080);
```

Let's say you have a need to test for the presence of a value:

```
$port = getSetting('port');

if (!$port->isSome()) {
    $log->debug("port setting empty");
    throw new InvalidArgumentException("port not provided");
}
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

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

Total

2

Last Release

2905d ago

PHP version history (2 changes)1.0.0PHP &gt;=7.1.0

1.1.0PHP &gt;=7.0.0

### Community

Maintainers

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

---

Top Contributors

[![senojj](https://avatars.githubusercontent.com/u/5853354?v=4)](https://github.com/senojj "senojj (11 commits)")

---

Tags

libraryoption

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[phpoption/phpoption

Option Type for PHP

2.7k541.2M159](/packages/phpoption-phpoption)[league/iso3166

ISO 3166-1 PHP Library

69536.3M116](/packages/league-iso3166)[payum/iso4217

ISO 4217 PHP Library

16312.1M5](/packages/payum-iso4217)[dekor/php-array-table

PHP Library for printing associative arrays as text table (similar to mysql terminal console)

296.6M2](/packages/dekor-php-array-table)

PHPackages © 2026

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