PHPackages                             okopok/optional - 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. okopok/optional

ActiveLibrary

okopok/optional
===============

Optional implementation for PHP

1.0.5(2y ago)1256MITPHPPHP &gt;=8.0CI passing

Since Jan 11Pushed 12mo ago1 watchersCompare

[ Source](https://github.com/okopok/optional)[ Packagist](https://packagist.org/packages/okopok/optional)[ Docs](https://t.me/okopok)[ RSS](/packages/okopok-optional/feed)WikiDiscussions master Synced 1mo ago

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

Php implementation of Java-9 Optional, with 'generics' support

Badges
======

[](#badges)

[![PHP Composer And Tests](https://github.com/okopok/optional/actions/workflows/php.yml/badge.svg)](https://github.com/okopok/optional/actions/workflows/php.yml)[![Qodana](https://github.com/okopok/optional/actions/workflows/qodana_code_quality.yml/badge.svg)](https://github.com/okopok/optional/actions/workflows/qodana_code_quality.yml)

Features
========

[](#features)

- Full 1:1 implementation of Java-9 Optional API
- Generic support (conceptually, obviously), for objects and primitives
- `OptionalBool`, `OptionalFloat`, `OptionalInt`, `OptionalString` already available
- Only 1 single class, optimized for performance
- Production ready

Usage
=====

[](#usage)

```
echo OptionalString::of('hello')->orElse('world');// echo 'hello'

echo OptonalInt::ofNullable(null)->orElse(42);// echo 42
```

Generics
========

[](#generics)

```
/**
 * @method Book get()
 * @method Book|null orElse($other)
 * @method Book orElseGet(callable $supplier)
 * @method Book orElseThrow(callable $exceptionSupplier)
 */
class OptionalBook extends AbstractOptional
{
    /**
     * @inheritdoc
     */
    protected function supports($value)
    {
        return $value instanceof Book;
    }
}

$book = new Book()
$optBook = OptionalBook::of($book);
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

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

Total

6

Last Release

766d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0d40f8856d87dbb0ecd25709de7db867890615a6241b5305b5ffc90782a1029e?d=identicon)[okopok](/maintainers/okopok)

---

Top Contributors

[![okopok](https://avatars.githubusercontent.com/u/237219?v=4)](https://github.com/okopok "okopok (14 commits)")

---

Tags

phpoptiongenericoptional

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/okopok-optional/health.svg)

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

###  Alternatives

[phpoption/phpoption

Option Type for PHP

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

PHPackages © 2026

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