PHPackages                             yitznewton/maybe-php - 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. yitznewton/maybe-php

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

yitznewton/maybe-php
====================

A Maybe monad implementation for PHP

0.2.2(11y ago)5108BSD-2-ClausePHPPHP &gt;=5.4

Since Nov 2Pushed 11y ago1 watchersCompare

[ Source](https://github.com/yitznewton/maybe-php)[ Packagist](https://packagist.org/packages/yitznewton/maybe-php)[ RSS](/packages/yitznewton-maybe-php/feed)WikiDiscussions master Synced 1mo ago

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

[![Travis build status](https://camo.githubusercontent.com/07dc13829535164db852f1f93d9de154249b0cef0cbd726023d58aff4693120b/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f7969747a6e6577746f6e2f6d617962652d7068702e737667)](https://camo.githubusercontent.com/07dc13829535164db852f1f93d9de154249b0cef0cbd726023d58aff4693120b/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f7969747a6e6577746f6e2f6d617962652d7068702e737667)[![PHP 5.3 not supported](https://camo.githubusercontent.com/9afd5c76b9f341454a74f4128de5dcd89ffc7751f93c76de0745cfd31004b286/687474703a2f2f696d672e736869656c64732e696f2f62616467652f352e332d6e6f745f737570706f727465642d7265642e737667)](https://camo.githubusercontent.com/9afd5c76b9f341454a74f4128de5dcd89ffc7751f93c76de0745cfd31004b286/687474703a2f2f696d672e736869656c64732e696f2f62616467652f352e332d6e6f745f737570706f727465642d7265642e737667)[![PHP 5.4 supported](https://camo.githubusercontent.com/760e693148ed706bbc61496cb5545c2bfd18102ac49bea0151ab20c1f55d2f5b/687474703a2f2f696d672e736869656c64732e696f2f62616467652f352e342d737570706f727465642d677265656e2e737667)](https://camo.githubusercontent.com/760e693148ed706bbc61496cb5545c2bfd18102ac49bea0151ab20c1f55d2f5b/687474703a2f2f696d672e736869656c64732e696f2f62616467652f352e342d737570706f727465642d677265656e2e737667)[![PHP 5.5 supported](https://camo.githubusercontent.com/d99bc53b22b8e1de05cf0a84f9a2cc3ebdabf0662d864baa6ad6cc06dfb68ebe/687474703a2f2f696d672e736869656c64732e696f2f62616467652f352e352d737570706f727465642d677265656e2e737667)](https://camo.githubusercontent.com/d99bc53b22b8e1de05cf0a84f9a2cc3ebdabf0662d864baa6ad6cc06dfb68ebe/687474703a2f2f696d672e736869656c64732e696f2f62616467652f352e352d737570706f727465642d677265656e2e737667)[![PHP 5.6 supported](https://camo.githubusercontent.com/2120621e988c8e3363fd474475bd059915df9eb8357791e2440a2e75ea4b7117/687474703a2f2f696d672e736869656c64732e696f2f62616467652f352e362d737570706f727465642d677265656e2e737667)](https://camo.githubusercontent.com/2120621e988c8e3363fd474475bd059915df9eb8357791e2440a2e75ea4b7117/687474703a2f2f696d672e736869656c64732e696f2f62616467652f352e362d737570706f727465642d677265656e2e737667)[![HHVM tested](https://camo.githubusercontent.com/95d6152cc8fc84b18e2ebbf0d0af311e32f4673b2d15313cd922c8f561ea2b60/687474703a2f2f696d672e736869656c64732e696f2f6868766d2f7969747a6e6577746f6e2f6d617962652d7068702e737667)](https://camo.githubusercontent.com/95d6152cc8fc84b18e2ebbf0d0af311e32f4673b2d15313cd922c8f561ea2b60/687474703a2f2f696d672e736869656c64732e696f2f6868766d2f7969747a6e6577746f6e2f6d617962652d7068702e737667)[![BSD 2-Clause license](https://camo.githubusercontent.com/f03b5ca1cb23ed2efab853c0cf40c51d344994d49707ee6842fe1dacab592e18/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7969747a6e6577746f6e2f6d617962652d7068702e737667)](https://camo.githubusercontent.com/f03b5ca1cb23ed2efab853c0cf40c51d344994d49707ee6842fe1dacab592e18/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7969747a6e6577746f6e2f6d617962652d7068702e737667)

A Maybe monad implementation for PHP
====================================

[](#a-maybe-monad-implementation-for-php)

This project was wholly inspired by [a blog post](http://linepogl.wordpress.com/2011/03/15/a-php-maybe-monad-2/)by @linepogl.

Motivation
----------

[](#motivation)

Dealing with `null` values (and, in PHP, falsy values) is tedious and prone to developer error (viz the null pointer exception, trying to dereference a `null`).

In my exposure to Haskell, I learned about the awesomeness of pattern matching, whereby you can get the compiler to force yourself to handle all possibilities. This combines with a tool called `Maybe` to require specific handling for "null" and "non-null" possibilities.

PHP does not offer pattern matching, but we can still use classes to wrap raw values, and require us to handle null conditions, without repeated explicit null checking and conditionals.

Examples
--------

[](#examples)

### Simple

[](#simple)

Before:

```
$blogpost = $repository->get($blogpostId);
echo $blogpost->teaser();  // oh noe! what if $blogpost is null?! :boom:
```

After:

```
$blogpost = new \Yitznewton\Maybe\Maybe($repository->get($blogpostId));
echo $blogpost->select(function ($bp) { $bp->teaser(); })->valueOr('No blogpost found');
```

### With callback

[](#with-callback)

```
$blogpost = new \Yitznewton\Maybe\Maybe($repository->get($blogpostId));
$callback = function () {
    return someExpensiveOperation();
};
echo $blogpost->select(function ($bp) { $bp->teaser(); })->valueOrCallback($callback);
```

### Loose-falsy

[](#loose-falsy)

```
// $process->execute() normally returns a result object, but sometimes returns false
$result = new LooseMaybe($process->execute());

echo $result->select(function ($resultObject) { $resultObject->getStatus(); })->valueOr('failed');
// echoes 'failed' when the result was false
```

Performance
-----------

[](#performance)

In a simple test using PHP 5.5, performance was approximately **20%** that of a straight `is_null()` check in an if/else conditional. In other words it takes 5 times as long to run.

You can reproduce the test locally by running the profiling testsuite in PHPUnit. You will first need to install XHProf, and override the XHProf lib directory using a local `phpunit.xml` config.

```
$ ./vendor/bin/phpunit --testsuite=profiling
```

Dictionary wrapper
------------------

[](#dictionary-wrapper)

maybe-php includes an array wrapper called `Dictionary`, whereby trying to access properties on the wrapper will return a `Maybe` object. You can specify whether to return a plain `Maybe` (default) or a `LooseMaybe`.

```
$dictionary = new \Yitznewton\Maybe\Dictionary([
    'foo' => 'bar',
]);

$dictionary->foo->valueOr('quux');        // 'bar'
$dictionary->noSuchKey->valueOr('quux');  // 'quux'

// with LooseMaybe

$dictionary = new \Yitznewton\Maybe\Dictionary([
    'foo' => false,
], \Yitznewton\Maybe\LooseMaybe::class);

$dictionary->foo->valueOr('quux'); // 'quux', because loose falsy
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

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

Total

4

Last Release

4211d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/80399c682aa6e0a353272015b221b5581bec505409017a175993bc719b4ae346?d=identicon)[yitznewton](/maintainers/yitznewton)

---

Top Contributors

[![yitznewton](https://avatars.githubusercontent.com/u/619299?v=4)](https://github.com/yitznewton "yitznewton (21 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/yitznewton-maybe-php/health.svg)

```
[![Health](https://phpackages.com/badges/yitznewton-maybe-php/health.svg)](https://phpackages.com/packages/yitznewton-maybe-php)
```

###  Alternatives

[ayesh/composer-preload

Preload your sweet sweet code to opcache with a composer command, making your code faster to run

209126.9k](/packages/ayesh-composer-preload)[alexwight/php-signalr-client

Signal R Client

2329.3k](/packages/alexwight-php-signalr-client)

PHPackages © 2026

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