PHPackages                             prewk/result - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. prewk/result

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

prewk/result
============

Result object for PHP inspired by Rust

4.1.0(9mo ago)120859.2k—4.6%14[9 PRs](https://github.com/prewk/result/pulls)9(MIT or Apache-2.0)PHPPHP &gt;=8.1.0CI passing

Since Apr 19Pushed 9mo ago6 watchersCompare

[ Source](https://github.com/prewk/result)[ Packagist](https://packagist.org/packages/prewk/result)[ RSS](/packages/prewk-result/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (7)Versions (30)Used By (9)

PHP Result Object [![Build Status](https://github.com/prewk/result/actions/workflows/test.yml/badge.svg)](https://github.com/prewk/result/actions) [![Coverage Status](https://camo.githubusercontent.com/cfb09bd3e47947688dd1b2027ae7bce0e02c6f834e6c8b6fa4e494a345b201ab/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f707265776b2f726573756c742f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/prewk/result?branch=master)
=================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#php-result-object--)

A PHP implementation of [Rust's Result type](https://doc.rust-lang.org/std/result/enum.Result.html) with roughly the same API.

Version information
-------------------

[](#version-information)

Version 4.x.x requires PHP 8.1.0+. Make sure you match the versions for this and the [Option](https://github.com/prewk/option) library if you use both.

Installation
------------

[](#installation)

```
composer require prewk/result
```

Overview
--------

[](#overview)

```
use Prewk\Result;
use Prewk\Result\{Ok, Err};

function someApiCall(): Result {
    // ...
    if ($apiCallSuccesful) {
        return new Ok($results);
    } else {
        return new Err($error);
    }
}

function anotherApiCall(): Result {
    // ...
    if ($apiCallSuccesful) {
        return new Ok($results);
    } else {
        return new Err($error);
    }
}

// Fallback to value
$value = someApiCall()->unwrapOr(null);

// Fallback to result and throw an exception if both fail
$value = someApiCall()->orElse(function($err) {
	return anotherApiCall();
})->unwrap();

// Throw custom exception on error
$value = someApiCall()->expect(new Exception("Oh noes!"));
```

Helpers
-------

[](#helpers)

Optional global helper functions exist to simplify result object construction:

```
ok(); // new Prewk\Result\Ok(null);
ok($val); // new Prewk\Result\Ok($val);
err($e); // new Prewk\Result\Err($e);
```

Add the following to your `composer.json`:

```
{
  "autoload": {
    "files": ["vendor/prewk/result/helpers.php"]
  }
}
```

API deviations from Rust
------------------------

[](#api-deviations-from-rust)

### Exceptions

[](#exceptions)

If an Err containing an `Exception` is unwrapped, that Exception will be thrown. Otherwise a generic `ResultException` will be thrown.

Gotchas
-------

[](#gotchas)

Note that `or` and `and` will be evaluated immediately:

```
// This will call all three api calls regardless of successes/errors
$this
	->apiCall()
	->or(anotherApiCall())
	->and(thirdApiCall());
```

See `andThen` and `orElse` for lazy evaluation.

License
-------

[](#license)

MIT &amp; Apache 2.0

###  Health Score

60

—

FairBetter than 99% of packages

Maintenance58

Moderate activity, may be stable

Popularity54

Moderate usage in the ecosystem

Community28

Small or concentrated contributor base

Maturity83

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 71.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~160 days

Total

20

Last Release

272d ago

Major Versions

0.0.5 → 1.0.02017-07-31

1.2.0 → 2.0.02018-03-19

2.1.1 → 3.0.02021-03-24

3.3.0 → 4.0.02024-05-10

PHP version history (3 changes)1.0.0PHP &gt;=7.0

3.0.0PHP &gt;=7.3.0

4.0.0PHP &gt;=8.1.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/640102?v=4)[Oskar Thornblad](/maintainers/prewk)[@prewk](https://github.com/prewk)

---

Top Contributors

[![prewk](https://avatars.githubusercontent.com/u/640102?v=4)](https://github.com/prewk "prewk (30 commits)")[![Lctrs](https://avatars.githubusercontent.com/u/5477973?v=4)](https://github.com/Lctrs "Lctrs (6 commits)")[![andrew-demb](https://avatars.githubusercontent.com/u/12499813?v=4)](https://github.com/andrew-demb "andrew-demb (3 commits)")[![Carlsson87](https://avatars.githubusercontent.com/u/5379989?v=4)](https://github.com/Carlsson87 "Carlsson87 (1 commits)")[![t1sh0o](https://avatars.githubusercontent.com/u/3911728?v=4)](https://github.com/t1sh0o "t1sh0o (1 commits)")[![thgs](https://avatars.githubusercontent.com/u/8940963?v=4)](https://github.com/thgs "thgs (1 commits)")

---

Tags

datastructuresphprust

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StyleECS

Type Coverage Yes

### Embed Badge

![Health badge](/badges/prewk-result/health.svg)

```
[![Health](https://phpackages.com/badges/prewk-result/health.svg)](https://phpackages.com/packages/prewk-result)
```

###  Alternatives

[honeybee/honeybee

Library for implementing CQRS driven, event-sourced and distributed architectures.

222.1k4](/packages/honeybee-honeybee)[rami/seo-bundle

All in One Symfony Seo Bundle

321.0k](/packages/rami-seo-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
