Skip to content
Option object for PHP inspired by Rust
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
spec/Option
src
.gitignore
.travis.yml
composer.json
composer.lock
helpers.php
phpspec.yml
psalm.xml
readme.md

readme.md

PHP Option Object Build Status Coverage Status

A PHP implementation of Rust's Option type with roughly the same API.

Installation

composer require prewk/option

Usage

use Prewk\Option;
use Prewk\Option\{Some, None};

function findSomething(): Option {
    // ...
    if ($foundSomething) {
        return new Some($thing);
    } else {
        return new None;
    }
}

function findSomethingElse(): Result {
    // ...
    if ($foundSomething) {
        return new Some($thing);
    } else {
        return new None;
    }
}

// Fallback to value
$value = findSomething()->unwrapOr(null);

// Fallback to option and throw an exception if both fail
$value = findSomething()->or(findSomethingElse())->unwrap();

// Throw custom exception on missing thing (None)
$value = findSomething()->expect(new Exception("Oh noes!"));

License

MIT & Apache 2.0

You can’t perform that action at this time.