PHPackages                             ocramius/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. ocramius/optional

Abandoned → [whsv26/functional](/?search=whsv26%2Ffunctional)ArchivedLibrary[Utility &amp; Helpers](/categories/utility)

ocramius/optional
=================

A port of java.util.Optional to PHP

1.0.0(7y ago)272634[6 PRs](https://github.com/Ocramius/ocramius.util.Optional/pulls)GPL-2.0PHPPHP ^7.3

Since Mar 12Pushed 4y ago1 watchersCompare

[ Source](https://github.com/Ocramius/ocramius.util.Optional)[ Packagist](https://packagist.org/packages/ocramius/optional)[ Docs](https://github.com/Ocramius/Optional)[ RSS](/packages/ocramius-optional/feed)WikiDiscussions master Synced 1mo ago

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

`ocramius\util\Optional`
========================

[](#ocramiusutiloptional)

[![Build Status](https://camo.githubusercontent.com/fdc24d7d391c347b94554c2f92f0e743c03c375df6442fafb1e119a42b199baa/68747470733a2f2f7472617669732d63692e6f72672f4f6372616d6975732f6f6372616d6975732e7574696c2e4f7074696f6e616c2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Ocramius/ocramius.util.Optional)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b8a0b560a7e241fa93cc62942c497acb347baff6980924b868235730edf59b19/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4f6372616d6975732f6f6372616d6975732e7574696c2e4f7074696f6e616c2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Ocramius/ocramius.util.Optional/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/f81526bc6fdca01cb6b88b9efb8f54f5c8be807c85237f30c2aff950ac36e1bb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4f6372616d6975732f6f6372616d6975732e7574696c2e4f7074696f6e616c2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Ocramius/ocramius.util.Optional/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/79d90714b5d14fd9d5133ca8eea537c6067811c106b02f9392c8d8cda1e355c2/68747470733a2f2f706f7365722e707567782e6f72672f6f6372616d6975732f6f7074696f6e616c2f762f737461626c652e737667)](https://packagist.org/packages/ocramius/optional)[![Latest Unstable Version](https://camo.githubusercontent.com/02ec3c81b8a3d60120b742bd4aa45f8ba0a35909a2bdbcd6c5dce0ce27854dc8/68747470733a2f2f706f7365722e707567782e6f72672f6f6372616d6975732f6f7074696f6e616c2f762f756e737461626c652e737667)](https://packagist.org/packages/ocramius/optional)[![License](https://camo.githubusercontent.com/dba88e92dc3b26b13d8b7981c82fa42d20ac5ab1cdaeb2acee2ea5630eb455a7/68747470733a2f2f706f7365722e707567782e6f72672f6f6372616d6975732f6f7074696f6e616c2f6c6963656e73652e737667)](https://packagist.org/packages/ocramius/optional)

This package is a PHP port of the `java.util.Optional` class in the [OpenJDK](http://hg.openjdk.java.net/lambda/lambda/jdk/file/tip/src/share/classes/java/util/Optional.java) libraries.

Currently maintained by [Niklas Schöllhorn](https://github.com/nschoellhorn), taken over 3rd March, 2019

You can find the API of `java.lang.Optional` in the [Java 8 API docs](http://docs.oracle.com/javase/8/docs/api/java/util/Optional.html).

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

[](#installation)

```
composer require ocramius/optional
```

Differences with the Java implementation
----------------------------------------

[](#differences-with-the-java-implementation)

Because of PHP's current limitations, I had to rewrite some bits of the Java implementation as follows:

- `Optional#empty()` is named `Optional#newEmpty()`, because `empty` is a reserved PHP keyword
- type-safety is not ensured at any time: generics have simply been stripped from the `Optional` implementation. This may change in future, but I don't plan to do it right now.
- `Optional#toString()` is named `Optional#__toString()` in accordance to [PHP magic methods naming](http://php.net/manual/en/language.oop5.magic.php#object.tostring)
- `Consumer`, `Predicate`, `Function` and `Supplier` arguments are simply `callable`, for simplicity and flexibility.

License
-------

[](#license)

Since this library is a direct port of the OpenJDK sources, I have to keep the original license in place, which is GPLv2 + ClassPath exceptions.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

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

2624d ago

### Community

Maintainers

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

---

Top Contributors

[![Ocramius](https://avatars.githubusercontent.com/u/154256?v=4)](https://github.com/Ocramius "Ocramius (65 commits)")[![nschoellhorn](https://avatars.githubusercontent.com/u/3925180?v=4)](https://github.com/nschoellhorn "nschoellhorn (26 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (9 commits)")[![aj-jaswanth](https://avatars.githubusercontent.com/u/7703387?v=4)](https://github.com/aj-jaswanth "aj-jaswanth (2 commits)")

---

Tags

optionaljava

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[nervo/yuicompressor

YUI Compressor is an open source tool that supports the compression of both JavaScript and CSS files. The JavaScript compression removes comments and white-spaces as well as obfuscates local variables using the smallest possible variable name. CSS compression is done using a regular-expression-based CSS minifier.

161.5M5](/packages/nervo-yuicompressor)[soluble/japha

Soluble PHP Java bridge integration

7343.5k3](/packages/soluble-japha)[javadev/moneytostr-russian

The tool to convert amount to words in Ukrainian/English/Russian languages

421.2k](/packages/javadev-moneytostr-russian)

PHPackages © 2026

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