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

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

haskellcamargo/php-maybe-monad
==============================

A PHP implementation of Haskell's Maybe monad

v1.0(10y ago)291.2k6MITPHP

Since Jun 23Pushed 9y ago3 watchersCompare

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

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

PHP Maybe Monad
---------------

[](#php-maybe-monad)

[![Build Status](https://camo.githubusercontent.com/2d95f0832be08e943d027484e24248df61b64a7f5cb66dab0e29d57bb8bea2ae/68747470733a2f2f7472617669732d63692e6f72672f6861736b656c6c63616d6172676f2f7068702d6d617962652d6d6f6e61642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/haskellcamargo/php-maybe-monad) [![Latest Stable Version](https://camo.githubusercontent.com/1824a940f1d97f66f9a70ea7d2d66ac6fffd1fef1b14d38ba9baaf3cf0106517/68747470733a2f2f706f7365722e707567782e6f72672f6861736b656c6c63616d6172676f2f7068702d6d617962652d6d6f6e61642f762f737461626c65)](https://packagist.org/packages/haskellcamargo/php-maybe-monad) [![Total Downloads](https://camo.githubusercontent.com/dbc54f2496d3ab15fad6b779e1664a9e3e01a3f3967164712ff457a76952360a/68747470733a2f2f706f7365722e707567782e6f72672f6861736b656c6c63616d6172676f2f7068702d6d617962652d6d6f6e61642f646f776e6c6f616473)](https://packagist.org/packages/haskellcamargo/php-maybe-monad) [![Latest Unstable Version](https://camo.githubusercontent.com/e28dba04e98844586ae3539dd0ac2c6fd3c4b391be00843e0f7d86aa367c4c38/68747470733a2f2f706f7365722e707567782e6f72672f6861736b656c6c63616d6172676f2f7068702d6d617962652d6d6f6e61642f762f756e737461626c65)](https://packagist.org/packages/haskellcamargo/php-maybe-monad) [![License](https://camo.githubusercontent.com/26ed0f24a556a83b8de351170871f3d42dd053200399a14f3cc0bce7e7e091ae/68747470733a2f2f706f7365722e707567782e6f72672f6861736b656c6c63616d6172676f2f7068702d6d617962652d6d6f6e61642f6c6963656e7365)](https://packagist.org/packages/haskellcamargo/php-maybe-monad)

To deal with computations that may fail. A port of Haskell's `Data.Maybe` module for **PHP &gt; 5.4**.

### Install

[](#install)

You can install via Composer.

```
composer require haskellcamargo/php-maybe-monad
```

### Example

[](#example)

```
use HaskellCamargo\Maybe;

Maybe\Maybe(@$_GET["username"])->bind(function($user)) {
  echo "Welcome, $user. You're logged in!";
});

$userAge = Maybe\Maybe(null)->fromMaybe(0); // => 0
$userAge = Maybe\Maybe(19)->fromMaybe(0); // => 19
```

### Documentation

[](#documentation)

A `Maybe` type encapsulates an optional value. A value of type `Maybe a`either contains a value of type a (represented as `Just a`), or it is empty (represented as `Nothing`). Using `Maybe` is a good way to deal with errors or exceptional cases without resorting to drastic measures such as `Exception`. The `Maybe` type is also a monad. It is a simple kind of error monad, where all errors are represented by `Nothing`. A richer error monad can be built using the `Either` type.

#### bind :: (Maybe a, callable) -&gt; Maybe b

[](#bind--maybe-a-callable---maybe-b)

Equivalent to Haskell's `>>=` operator. Its first argument is a value in a monadic type, its second argument is a function that maps from the underlying type of the first argument to another monadic type, and its results is in that other monadic type.

```
$age = Maybe\Maybe(null)->bind(function($x) {
  return 10;
}); // => Nothing

$age = Maybe\Maybe(10)
->bind(function($x) {
  return $x + 10; // => Just(20);
})
->bind(function($x) {
  return $x + 20; // => Just(40);
})->fromJust(); // => 40
```

#### fromJust :: Maybe a -&gt; a

[](#fromjust--maybe-a---a)

Extracts the element out of a `Just` and returns an error if its argument is `Nothing`.

```
Maybe\Maybe("Foo")->fromJust(); // => "Foo"
Maybe\Maybe(null)->fromJust(); // => Exception: Cannot cal fromJust() on Nothing
```

#### fromMaybe :: (Maybe a, a) -&gt; a

[](#frommaybe--maybe-a-a---a)

Takes a `Maybe` value and a default value. If the `Maybe` is `Nothing`, it returns the default values; otherwise, it returns the value contained in the `Maybe`.

```
Maybe\Maybe(10)->fromMaybe(5); // => 10
Maybe\Maybe(null)->fromMaybe(5); // => 5
```

#### isJust :: Maybe a -&gt; boolean

[](#isjust--maybe-a---boolean)

Returns true if its argument is of the form `Just _`.

```
Maybe\Maybe(10)->isJust(); // => true
Maybe\Maybe(null)->isJust(); // => false
```

#### isNothing :: Maybe a -&gt; boolean

[](#isnothing--maybe-a---boolean)

Returns true if its argument is of the form `Nothing`.

```
Maybe\Maybe(10)->isNothing(); // => false
Maybe\Maybe(null)->isNothing(); // => true
```

#### maybe :: (Maybe a, b, callable) -&gt; b

[](#maybe--maybe-a-b-callable---b)

Takes a default value, a function and, of course, a `Maybe` value. If the `Maybe` value is `Nothing`, the function returns the default value. Otherwise, it applies the function to the value inside the `Just` and returns the result.

```
$just = Maybe\Maybe(10);
$nothing = Maybe\Maybe(null);

$just->maybe(40, function($num) {
        return $num + 15;
}); // => 25

$nothing->maybe(40, function($num) {
  return $num + 15;
}); // => 40
```

#### toList :: Maybe a -&gt; array

[](#tolist--maybe-a---array)

Returns an empty list when given `Nothing` or a singleton list when not given `Nothing`.

```
Maybe\Maybe(10)->toList(); // => [10]
Maybe\Maybe(null)->toList(); // => []
```

Made with ❤️ by Marcelo Camargo and Reinaldo Rauch

### License

[](#license)

MIT

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

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

3982d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/75b67763139f7a8c96302299f5a40a386d565829af3061bb55465c8808d54428?d=identicon)[sergiors](/maintainers/sergiors)

![](https://www.gravatar.com/avatar/3ec5622b57e31fe1e05f92307f7556c6b7599d6b1458203377a704f124aca983?d=identicon)[haskellcamargo](/maintainers/haskellcamargo)

---

Top Contributors

[![haskellcamargo](https://avatars.githubusercontent.com/u/7553006?v=4)](https://github.com/haskellcamargo "haskellcamargo (23 commits)")[![reinaldorauch](https://avatars.githubusercontent.com/u/2676370?v=4)](https://github.com/reinaldorauch "reinaldorauch (11 commits)")[![sergiors](https://avatars.githubusercontent.com/u/2046276?v=4)](https://github.com/sergiors "sergiors (4 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[phpwhois/phpwhois

phpWhois - library for querying whois services and parsing results. Based on phpwhois.org

322392.6k1](/packages/phpwhois-phpwhois)[cakedc/tiny-mce

TinyMCE Plugin for CakePHP

10790.2k](/packages/cakedc-tiny-mce)[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21421.6k](/packages/imanghafoori-laravel-anypass)[rasteiner/k3-whenquery

Conditionally show fields and sections. Better.

6717.8k](/packages/rasteiner-k3-whenquery)[dragon-code/size-sorter

Easily sort clothing size, height, bra size, furniture size and more

3915.4k](/packages/dragon-code-size-sorter)[geeklabs/ci4-breadcrumbs

Breadcrumb navigation for CodeIgniter 4

2813.5k](/packages/geeklabs-ci4-breadcrumbs)

PHPackages © 2026

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