sayuprc / result-type
Library for handling result types in PHP
Installs: 1 421
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/sayuprc/result-type
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.