sayuprc / result-type
Library for handling result types in PHP
0.4.0
2025-06-28 10:48 UTC
Requires
- php: ^8.4
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^12.1
- symplify/easy-coding-standard: ^12.5
README
A library for handling result types in PHP.
Requirements
name | version |
---|---|
PHP | ^8.4 |
Installation
composer require sayuprc/result-type
Usage
<?php use ResultType\Eager\Err; use ResultType\Eager\Ok; use ResultType\Result; class Success { // Some data } class Error { // Some error } class Handler { /** * @return Result<Success, Error> */ function handle(): Result { if (/* some error */) { return new Err(new Error()); } return new Ok(new Success()); } } $handler = new Handler(); $result = $handler->handle(); if ($result->isOk()) { $result->unwrap(); // Access to Success } else { $result->unwrapErr(); // Access to Error }
Wiki
For more information, please click here.