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

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

sassnowski/option
=================

A basic PHP implementation of the Option data type as found in Scala.

0.2.0(10y ago)111MITPHPPHP &gt;=5.5

Since May 3Pushed 9y agoCompare

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

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

Option Type for PHP
===================

[](#option-type-for-php)

[![Build Status](https://camo.githubusercontent.com/2fdc0dfea1d7c70c0671430203395adcf9a33627df3103ee368a3354bd34c42e/68747470733a2f2f7472617669732d63692e6f72672f6b736173736e6f77736b692f6f7074696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ksassnowski/option)[![Coverage Status](https://camo.githubusercontent.com/15169ac9afb1067e50538fc9ef08f199b370b989b704ae125e4947b4dcf756fd/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6b736173736e6f77736b692f6f7074696f6e2f62616467652e7376673f6272616e63683d646576656c6f70)](https://coveralls.io/github/ksassnowski/option?branch=develop)[![Code Climate](https://camo.githubusercontent.com/5d803985791442d402b4df8fdfe871b18f9af3ea99075e6d9cbc06571f788593/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6b736173736e6f77736b692f6f7074696f6e2f6261646765732f6770612e737667)](https://codeclimate.com/github/ksassnowski/option)[![SensioLabsInsight](https://camo.githubusercontent.com/6a3dfa2f2de4573fa08ae3b1f8bc09cb539d7f8515481fe3d1182435fe35d3fa/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f33663939616563352d656463662d346430322d613561652d3436366238333638306464372f6d696e692e706e67)](https://insight.sensiolabs.com/projects/3f99aec5-edcf-4d02-a5ae-466b83680dd7)

A PHP implementation of the `Option` data type from Scala (or `Maybe` from Haskell if you want).

**DISCLAIMER: This package is not intented to be used in production. Its intend was to serve as a coding exercise to myself about how I would implement and Option Type in PHP. If you want to use this in production I'd recommend you use  instead.**

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

[](#installation)

Install the package through composer:

```
$ composer require sassnowski/option
```

That’s it! Now you can use it in your code.

```
function divide($a, $b)
{
	if (0 === $b)
	{
		return Option::None();
	}

	return new Option($a / $b);
}

$result = divide(10, 5);
$result->get(); // 2

$result2 = divide(10, 0);
$result->isDefined(); // false
```

Summary
-------

[](#summary)

An `Option` represents an optional value, or in other words a value that may not exist. It is sometimes described as a List that contains a maximum of one item.

An `Option` is used in places where otherwise `null` might be used, e.g., the result of a Database Query. A more general way to put it is: A computation might return an `Option` if it is not defined for some inputs.

Option::map($func)
------------------

[](#optionmapfunc)

Using `Option`s means that a lot of code needs to be aware of this data type. In order to still be able to reuse functions that operate on unwrapped values, this class provides a `map` function.

The purpose of the `map` function is to *lift* a function that normally operators on regular values to now work on `Option` values. Formally it turns a function of type

```
a -> b

```

into a function of type

```
Option a -> Option b

```

### Example

[](#example)

```
// Note: This example uses PHP 7 type hinting. This is in no
// way necessary for this package to work and is simply there
// to illustrate the types that these functions are supposed to
// operate on.

function length(string $a): int
{
	return strlen($a);
}

$length1 = (new Option("abc"))->map('length');
$length1->isDefined(); // true
$length1->get(); // 3

// The length function never gets executed, since we're
// dealing with an undefined value.
$length2 = Option::None->map('length');
$length2->isDefined(); // false
$length2->get(); // RuntimeException
```

The above example lifted the function `length` of type `string -> int` into a function of type `Option string -> Option int`. This means that we can still write and use functions that were written without optional values in mind and simply lift them to a function that can handle `Option`s.

An important characteristic of the `map` method is, that the function that is being mapped over the option will never get executed if we’re dealing with an undefined value.

Option::flatMap($func)
----------------------

[](#optionflatmapfunc)

*Todo*

Option::getOrElse($default)
---------------------------

[](#optiongetorelsedefault)

*Todo*

Option::orElse($alternative)
----------------------------

[](#optionorelsealternative)

*Todo*

Option::isDefined()
-------------------

[](#optionisdefined)

This function simply returns `true` if the value is anything other than `null` in which case it returns `false`.

License
-------

[](#license)

MIT

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

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

Total

2

Last Release

3710d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4502ad7427ecdff776d65d1ba32c9db22d0b9510a7cc959cab93fa0f5fa138de?d=identicon)[ksassnowski](/maintainers/ksassnowski)

---

Top Contributors

[![ksassnowski](https://avatars.githubusercontent.com/u/5139098?v=4)](https://github.com/ksassnowski "ksassnowski (24 commits)")

### Embed Badge

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

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

###  Alternatives

[knplabs/knp-components

Knplabs component library

77245.0M48](/packages/knplabs-knp-components)[symfony/polyfill-php85

Symfony polyfill backporting some PHP 8.5+ features to lower PHP versions

5175.5M234](/packages/symfony-polyfill-php85)[customgento/module-cookiebot-m2

This Magento 2 module integrates Cookiebot into your store.

2468.3k](/packages/customgento-module-cookiebot-m2)[diarmuidie/envpopulate

Tool to interactively populate a `.env` file based on an `.env.example` file whenever Composer installs or updates.

1695.7k](/packages/diarmuidie-envpopulate)

PHPackages © 2026

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