o / result
A basic port of the Result type from Rust
dev-master
2020-01-29 04:33 UTC
Requires
- php: >=7.1
- o/option: dev-master
Requires (Dev)
- kahlan/kahlan: ^4.7
- phpstan/phpstan: ^0.12.7
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2025-06-29 01:42:02 UTC
README
This is a partial port of the Result 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\Result\{ResultInterface, Ok, Err}; // basic setting and getting of values $greeting = new Ok('hey there'); $name = new Err(''); echo $greeting->unwrap(); // echos 'hey there' echo $name->unwrap(); // throws a ResultException echo $name->unwrapOr('unknown'); // echos 'unknown' // function that returns a Result<number, string> function divide(int $x, int $y): ResultInterface { if ($y === 0) { return new Err('cannot divide by zero'); } return new Ok(x / y); } divide(1, 0); // Err('cannot divide by zero') divide(1, 1); // Ok(1)
Linting
$ composer lint
Analysing
$ composer analyse
Testing
$ composer test:unit