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

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

yceruto/option-type
===================

An Option type that represents an optional value

v1.0.6(1y ago)1515MITPHPPHP &gt;=8.2

Since Apr 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/yceruto/option-type)[ Packagist](https://packagist.org/packages/yceruto/option-type)[ GitHub Sponsors](https://github.com/yceruto)[ RSS](/packages/yceruto-option-type/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (3)Versions (8)Used By (0)

PHP Option type
===============

[](#php-option-type)

[![Latest Stable Version](https://camo.githubusercontent.com/b4b5a39f4bc76a2854b90017c890471c773f4bec6ed20ca087c1073881647337/68747470733a2f2f706f7365722e707567782e6f72672f7963657275746f2f6f7074696f6e2d747970652f763f763d31)](https://packagist.org/packages/yceruto/option-type)[![Unstable](https://camo.githubusercontent.com/5c8d9b8129275d663963bb92e6dacde1905a89a576ff4e70f425667c92b59ab6/687474703a2f2f706f7365722e707567782e6f72672f7963657275746f2f6f7074696f6e2d747970652f762f756e737461626c65)](https://packagist.org/packages/yceruto/option-type)[![License](https://camo.githubusercontent.com/f49c5c6ea359281dc75e28d9dfe368e13459809f767a766876e3b28d468f5f41/68747470733a2f2f706f7365722e707567782e6f72672f7963657275746f2f6f7074696f6e2d747970652f6c6963656e7365)](https://packagist.org/packages/yceruto/option-type)[![PHP Version Require](https://camo.githubusercontent.com/7594bccd176a7cb7b6f4af2aa086b26004890ad9b19206b81af1ccc945fff3ee/68747470733a2f2f706f7365722e707567782e6f72672f7963657275746f2f6f7074696f6e2d747970652f726571756972652f706870)](https://packagist.org/packages/yceruto/option-type)

The `Option` type represents a value that might or might not be there. It's all about null safety in PHP!

Note

Inspired by [Rust's Option type](https://doc.rust-lang.org/std/option/) and other languages like Scala, Swift, F#, etc.

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

[](#installation)

```
composer require yceruto/option-type
```

Handling the presence or absence of a value with `null`
-------------------------------------------------------

[](#handling-the-presence-or-absence-of-a-value-with-null)

In PHP, denoting the absence of a value is done with `null`, e.g. when a `divide`function returns `null` if the divisor is `0`.

```
function divide(int $dividend, int $divisor): ?int
{
    if (0 === $divisor) {
        return null;
    }

    return intdiv($dividend, $divisor);
}

function success(int $result): string {
    return sprintf('Result: %d', $result);
}

$result = divide(10, 2);

echo success($result);
```

Can you spot the issue in this code? Apparently, everything is fine until you try to divide by zero. The function will return `null`, and the `success()` function will throw a `TypeError` because it expects an `int` value, not `null`.

The issue with this approach is that it's too easy to overlook checking if the value is `null`, leading to runtime errors, and this is where the `Option` type comes in handy: it always forces you to deal with the `null` case.

Handling the presence or absence of a value with `Option`
---------------------------------------------------------

[](#handling-the-presence-or-absence-of-a-value-with-option)

Options often work with pattern matching to check if there’s a value and act accordingly, always making sure to handle the `null` case.

```
use Std\Type\Option;
use function Std\Type\Option\none;
use function Std\Type\Option\some;

/**
 * @return Option
 */
function divide(int $dividend, int $divisor): Option
{
    if (0 === $divisor) {
        return none();
    }

    return some(intdiv($dividend, $divisor));
}

function success(int $result): string {
    return sprintf('Result: %d', $result);
}

// The return value of the function is an Option
$result = divide(10, 2);

// Pattern match to retrieve the value
echo $result->match(
    // The division was valid
    some: fn (int $v) => success($v),
    // The division was invalid
    none: fn () => 'Division by zero!',
);
```

Tip

You can use the functions `some()` and `none()` as quick ways to create an `Option`instance. `some()` is just like `new Some()`, meaning it includes a value, while `none()` is the same as `new None()`, indicating it is missing a value.

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

[](#documentation)

- [API Reference](docs/api_reference.md)
- [Examples](docs/examples.md)

License
-------

[](#license)

This software is published under the [MIT License](LICENSE)

###  Health Score

32

—

LowBetter than 71% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

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

Recently: every ~27 days

Total

7

Last Release

642d ago

### Community

Maintainers

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

---

Top Contributors

[![yceruto](https://avatars.githubusercontent.com/u/2028198?v=4)](https://github.com/yceruto "yceruto (43 commits)")

---

Tags

null-safetyoption-typeoptional-typephp

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

PHPackages © 2026

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