Maybe, Option and Result monads.

0.2.0 2021-08-07 00:00 UTC

This package is auto-updated.

Last update: 2021-12-05 19:56:35 UTC


Maybe, Option and Result monads for PHP.



I published on reddit an RFC proposal for what I called "enums constructor" (which happened to be an existing concept called "tagged unions"). Out of one comment, I shown how to chain calls to various ADT outcomes, in order to reduces pyramidal matches.

This got me into wanting to use more monads right now on my next projects. After searching on, I did not found something that fits what I expected, so I decided to make my own version of how I think monads should look like in PHP.


  • Supports theses monads:
    • Option
    • Maybe
    • Result
  • PHPStan and static analyzers friendly


  • PHP 8+ installed


composer require khalyomede/monad


1. Getting a file content

In this example, we will use the Maybe monad as result, and use it in our main code.

use Khalyomede\Monad\Maybe;

function getFileContent(string $filePath): Maybe
  $content = file_get_contents($filePath);

  return $content === false ? Maybe::nothing() : Maybe::just($content);

// main
$content = getFileContent("data.txt")
  ->then(fn (string $data): string => $data)
  ->catch(fn (): string => "N/A");

echo $content;

2. Get users from an SQL table

In this example, we will use the Result monad to return the list of users or an error from PDO.

use Khalyomede\Monad\Result;

function getUsers(): Result
  $pdo = new PDO("sqlite::memory:");

  $statement = $pdo->prepare("SELECT * FROM users");

  if ($statement === false) {
    $message = $pdo->errorInfo()[2];

    return Result::error($message);

  $result = $statement->execute();

  if ($result === false) {
    $message = $statement->errorInfo()[2];

    return Result::error($message);

  return Result::ok($statement->fetchAll());

// main
$users = getUsers()
  ->then(fn (array $records): array => $records)
  ->catch(function (string $error): array {

    return [];

3. Chain multiple outcomes

In this example, we will chain multiple times "then", similar to what is possible in other languages through "map".

use Khalyomede\Monad\Result;

function getFileContent(string $path): Result
  $content = file_get_contents($path);

  return $content === false ?
    Result::error(new Exception("Cannot get content of file $path.")) :

function saveFileContent(string $path, string $content): Result
  $result = file_put_contents($path, $content);

  return $result === false ?
    Result::error(new Exception("Cannot write file content of file $path.")) :

// main
$result = fileGetContent("composer.json")
  ->then(fn (string $content): Result => saveFileContent("", $content))
  ->then(fn (): string => "composer.json backup finished.")
  ->catch(fn (Exception $exception): string => $exception->getMessage());

echo $result . PHP_EOL;
khalyomede@pc > php index.php
composer.json backup finished.

If you remove the file, the result becomes

khalyomede@pc > php index.php
Cannot get content of file composer.json.

If you remove the write permission on this file, the result becomes

khalyomede@pc > php index.php
Cannot write file content of file


For a list of all possible functions, see the tests\unit folder.


composer run test
composer run mutate
composer run analyse
composer run check
composer outdated --direct