prewk / option
Option object for PHP inspired by Rust
4.1.0
2025-08-15 19:01 UTC
Requires
- php: >=8.1.0
- prewk/result: >= 1.2.0
Requires (Dev)
- ergebnis/composer-normalize: 2.42.0
- infection/infection: 0.27.11
- phpunit/phpunit: 10.5.15
- psalm/plugin-phpunit: 0.19.0
- symplify/easy-coding-standard: 12.1.14
- vimeo/psalm: 5.24.0
This package is auto-updated.
Last update: 2026-05-19 20:34:24 UTC
README
A PHP implementation of Rust's Option type with roughly the same API.
Version information
Version 4.x.x requires PHP 8.1+. Make sure you match the versions for this and the Result library if you use both.
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