PHPackages                             php-fp/php-fp-either - 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. php-fp/php-fp-either

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

php-fp/php-fp-either
====================

An implementation of the Either monad in PHP.

1.0.0(10y ago)4724.4k31MITPHP

Since Apr 19Pushed 9y agoCompare

[ Source](https://github.com/php-fp/php-fp-either)[ Packagist](https://packagist.org/packages/php-fp/php-fp-either)[ Docs](http://www.github.com/php-fp/php-fp-either)[ RSS](/packages/php-fp-php-fp-either/feed)WikiDiscussions master Synced today

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

The Either Monad for PHP. [![Build Status](https://camo.githubusercontent.com/421a8e4677a2cdf1432641f18470ef099df26ff988e1db09512a3a8670c619bd/68747470733a2f2f7472617669732d63692e6f72672f7068702d66702f7068702d66702d6569746865722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/php-fp/php-fp-either)
=====================================================================================================================================================================================================================================================================================================================

[](#the-either-monad-for-php-)

Intro
-----

[](#intro)

When you throw an exception in PHP, you effectively perform a `GOTO`: your position in the program's execution jumps to the appropriate exception handler, and execution continues. This is fine, but it obviously means that your original function has a side-effect: calling it will fundamentally alter the program flow. What we need is a *functional* way of accomplishing the same thing.

Enter, the Either monad. `Either` has two constructors, `Left` and `Right`. These work very similarly to `Maybe`'s `Just` and `Nothing`: `map`, `ap`, and `chain` work as you'd expect on the `Right` instance, but are effectively no-ops on the `Left`. However, the difference is that `Left`, unlike `Nothing`, holds a value.

This means that, typically, your left branch will hold the 'exception', and the right will hold the value. If an exception happens, all future computations are ignored, and the exception can be handled in a pure way. For example:

```
