o / option
A basic port of the Option type from Rust
Installs: 4
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/o/option
Requires
- php: >=7.1.3
Requires (Dev)
- kahlan/kahlan: ^4.6
- phpstan/phpstan: ^0.12.7
- squizlabs/php_codesniffer: ^3.5
- vimeo/psalm: ^3.11
This package is auto-updated.
Last update: 2025-12-22 00:11:26 UTC
README
This is a partial port of the Option type from Rust. Most of the functionality is here but I skipped porting some of the more Rust-specific methods that don't really make sense in a PHP context.
Usage
Basic usage is the same as in Rust.
use O\Option\{Some, None, OptionInterface}; // basic setting and getting of values $greeting = new Some('hey there'); $name = new None(); echo $greeting->unwrap(); // echos 'hey there' echo $name->unwrap(); // throws an OptionException echo $name->unwrapOr('unknown'); // echos 'unknown' function divide(int $x, int $y): OptionInterface { if (y === 0) { return new None(); } return new Some(x / y); } divide(1, 0); // None divide(1, 1); // Some(1)
Linting
$ composer lint
Analyzing
$ composer analyze
Testing
$ composer test:unit