<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/i-built-a-rust-style-result-type-for-php-8ct9ogr3y" -->

---
title: I built a Rust-style Result type for php | daily.dev
description: A developer created Result PHP, a dependency-free Result/Either type library for PHP 8.1+ inspired by Rust's error-handling model. It provides methods like...
canonical: https://daily.dev/posts/i-built-a-rust-style-result-type-for-php-8ct9ogr3y
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: I built a Rust-style Result type for php | daily.dev
og:description: A developer created Result PHP, a dependency-free Result/Either type library for PHP 8.1+ inspired by Rust's error-handling model. It provides methods like...
og:url: https://daily.dev/posts/i-built-a-rust-style-result-type-for-php-8ct9ogr3y
og:image: https://api.daily.dev/og/posts/8Ct9OgR3y.png
og:image:alt: I built a Rust-style Result type for php
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# I built a Rust-style Result type for php

**[PHP Dev](https://daily.dev/sources/phpdev)** · [@prabhnoorsingh44](https://daily.dev/prabhnoorsingh44) · 2 min read · 2 upvotes · 0 comments

## Summary

A developer created Result PHP, a dependency-free Result/Either type library for PHP 8.1+ inspired by Rust's error-handling model. It provides methods like map(), andThen(), orElse(), match(), Result::try(), Result::all(), zip(), and flatten() to make failure an explicit, composable part of function signatures instead of relying on exceptions or null/false returns. The library includes PHPStan generics for static typing and serialization support, and the author is soliciting feedback from PHP developers on whether they'd adopt this pattern over traditional exceptions in frameworks like Laravel or Symfony.

## Content

One thing I’ve always found awkward in PHP is error handling.

You either end up throwing exceptions everywhere, or returning null/false and hoping the caller remembers to check it.

So I built Result PHP — a small, dependency-free Result/Either implementation for modern PHP.

The idea is simple:

function findUser(int $id): Result

{

$user = User::find($id);

return $user

? Result::success($user)

: Result::failure('User not found');

}

Now the caller can explicitly handle both outcomes:

findUser(42)->match(

success: fn(User $user) => showProfile($user),

failure: fn(string $error) => abort(404, $error)

);

And Results can be composed:

$result = findUser(42)

->andThen(fn($user) => validatePermissions($user))

->map(fn($user) => $user->toArray())

->orElse(fn($error) => fetchFromCache())

->getOrElse([]);

Some of the things I included:

map() / mapError()

andThen() for chaining

orElse() for fallbacks

match() for explicit handling

Result::try() for wrapping exceptions

Result::all() and collect() for combining Results

zip() and flatten()

Serialization support

Immutable operations

PHPStan generics for better static type information

PHP 8.1+

Zero external dependencies

The goal isn't to make PHP behave exactly like Rust.

It's to bring one useful idea from Rust's type system into everyday PHP development: make failure an explicit part of the API instead of an unexpected side effect.

I'm still developing it and would love feedback from PHP developers:

Would you actually use a Result type like this in a Laravel/Symfony application, or do you prefer traditional exceptions?

GitHub: https://github.com/prabhnoor12/result-php

Composer:

composer require prabhnoor12/result-php

#php #laravel #symfony #opensource #programming #webdevelopment

## Similar posts on daily.dev

- [Result Pattern in C\# and .NET 10 - When to Use, When to Skip](https://daily.dev/posts/result-pattern-in-c-and-net-10---when-to-use-when-to-skip-qwyhjumgg) · Code with Mukesh · 59 upvotes · 1 comments
- [v0.9 of Gleam, a statically typed language for the Erlang VM, is out](https://daily.dev/posts/v0-9-of-gleam-a-statically-typed-language-for-the-erlang-vm-is-out-pkubrjwkn) · Lobsters · 1 upvotes · 0 comments
- [Result is the most boring sum type](https://daily.dev/posts/result-is-the-most-boring-sum-type-jtvdlei3c) · ploeh blog · 1 upvotes · 0 comments

---

Tags: [#php](https://daily.dev/tags/php), [#rust](https://daily.dev/tags/rust), [#functional-programming](https://daily.dev/tags/functional-programming)

[View this post on daily.dev](https://daily.dev/posts/i-built-a-rust-style-result-type-for-php-8ct9ogr3y)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/i-built-a-rust-style-result-type-for-php-8ct9ogr3y","headline":"I built a Rust-style Result type for php","text":"A developer created Result PHP, a dependency-free Result/Either type library for PHP 8.1+ inspired by Rust's error-handling model. It provides methods like map(), andThen(), orElse(), match(), Result::try(), Result::all(), zip(), and flatten() to make failure an explicit, composable part of function signatures instead of relying on exceptions or null/false returns. The library includes PHPStan generics for static typing and serialization support, and the author is soliciting feedback from PHP developers on whether they'd adopt this pattern over traditional exceptions in frameworks like Laravel or Symfony.","url":"https://daily.dev/posts/i-built-a-rust-style-result-type-for-php-8ct9ogr3y","datePublished":"2026-09-27T12:35:55.495Z","dateModified":"2026-09-27T12:36:13.450Z","author":{"@type":"Person","name":"PrabhnoorSingh","url":"https://daily.dev/prabhnoorsingh44","image":"https://media.daily.dev/image/upload/s--O0TOmw4y--/f_auto/v1715772965/public/noProfile","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":20}},"interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/squads/phpdev","name":"PHP Dev"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"PHP Dev","item":"https://daily.dev/squads/phpdev"},{"@type":"ListItem","position":3,"name":"I built a Rust-style Result type for php"}]}
```

