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

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

aphonix/option
==============

A zero-dependency, type-safe implementation of Rust's Option type for PHP 7.1+. Handle optional values with elegance.

v1.0.0(4mo ago)2199MITPHPPHP &gt;=7.1

Since Jan 7Pushed 4mo ago1 watchersCompare

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

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

**🦀 Rust-style Option for PHP 🐘**
=================================

[](#-rust-style-option-for-php-)

A lightweight, zero-dependency, and type-safe implementation of Rust's Option&lt;T&gt; for PHP 7.1+. This library brings functional programming patterns to PHP, helping you eliminate null pointer exceptions and write more expressive, safer code.

**✨ Features**
--------------

[](#-features)

- **PHP 7.1+ Compatibility**: Fully supports all versions from PHP 7.1 up to PHP 8.4+.
- **Strict Rust API**: Ported core methods from the Rust Standard Library (std::option).
- **Singleton None**: Memory-efficient implementation using a singleton for the None type.
- **Global Helper Functions**: Idiomatic Some() and None() functions for a clean development experience.
- **Type Safety**: Encourages explicit handling of optional values, removing hidden null reference errors.

**🚀 Installation**
------------------

[](#-installation)

You can install the package via [Composer](https://getcomposer.org/):

```
composer require aphonix/option
```

**📖 Quick Start**
-----------------

[](#-quick-start)

### **Basic Usage**

[](#basic-usage)

```
use function Aphonix\\Option\\{Some, None};

// Wrap a value that exists
$some \= Some("Aphonix");

// Represent the absence of a value
$none \= None();

if ($some-\>is\_some()) {
    echo $some-\>unwrap(); // Output: Aphonix
}
```

### **Functional Chaining**

[](#functional-chaining)

Avoid defensive if (is\_null($var)) nests with monad-style chaining:

```
$result = Some("  hello world  ")
    ->map('trim')
    ->map('strtoupper')
    ->filter(function ($s) {
        return strlen($s) > 5;
    })
    ->unwrap_or("DEFAULT");

echo $result; // Output: HELLO WORLD
```

### **Advanced: and\_then (FlatMap)**

[](#advanced-and_then-flatmap)

Use and\_then when your transformation closure returns an Option itself:

```
function find_user($id) {
    $users = [1 => ['name' => 'Alice']];
    return isset($users[$id]) ? Some($users[$id]) : None();
}

$name = Some(1)
    ->and_then('find_user')
    ->map(function ($user) { return $user['name']; })
    ->unwrap_or("Unknown");

echo $name; // Output: Alice
```

**🛠 API Reference**
-------------------

[](#-api-reference)

### **Check Methods**

[](#check-methods)

MethodDescription`is_some(): bool`Returns true if the option is a Some value.`is_none(): bool`Returns true if the option is a None value.`is_some_and(callable $f): bool`Returns true if the option is Some and the value inside matches the predicate.### **Unwrapping Methods**

[](#unwrapping-methods)

MethodDescriptionunwrap()Returns the inner value. Throws an Exception (Panic) if the value is None.expect(string $msg)Returns the inner value. Throws an Exception with a custom message if the value is None.unwrap\_or($default)Returns the inner value if it exists, otherwise returns the provided default.unwrap\_or\_else(callable $f)Returns the inner value if it exists, otherwise returns the result of the closure.### **Transformation &amp; Filtering**

[](#transformation--filtering)

MethodDescriptionmap(callable $f): OptionMaps an Option&lt;T&gt; to Option&lt;U&gt; by applying a function to the contained value.map\_or($default, callable $f)Returns the provided default result, or applies a function to the contained value.filter(callable $f): OptionReturns None if the option is Some and the closure returns false.and\_then(callable $f): OptionReturns None if the option is None, otherwise calls the closure with the value and returns the result.### **Logical Operations**

[](#logical-operations)

MethodDescriptionand(Option $optB): OptionReturns None if the option is None, otherwise returns $optB.or(Option $optB): OptionReturns the option if it contains a value, otherwise returns $optB.xor(Option $optB): OptionReturns Some if exactly one of self, $optB is Some, otherwise returns None.**🧪 Testing**
-------------

[](#-testing)

This project uses PHPUnit to ensure 100% logic coverage:

```
composer install
./vendor/bin/phpunit tests
```

**📄 License**
-------------

[](#-license)

The MIT License (MIT). Please see the [License File](https://www.google.com/search?q=LICENSE) for more information.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance76

Regular maintenance activity

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity29

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

132d ago

### Community

Maintainers

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

---

Top Contributors

[![bqdove](https://avatars.githubusercontent.com/u/13446550?v=4)](https://github.com/bqdove "bqdove (1 commits)")

---

Tags

phpoptionmonadrustnonetype-safetysome

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[phpoption/phpoption

Option Type for PHP

2.7k541.2M159](/packages/phpoption-phpoption)[chippyash/monad

Functional programming Monad support

2828.1k8](/packages/chippyash-monad)

PHPackages © 2026

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