ghostwriter/result

Provides a Result type implementation for PHP

Maintainers

Package info

github.com/ghostwriter/result

pkg:composer/ghostwriter/result

Fund package maintenance!

ghostwriter

Paypal

Statistics

Installs: 3 859 486

Dependents: 5

Suggesters: 0

Stars: 2

Open Issues: 0

2.0.0 2025-02-10 06:12 UTC

This package is auto-updated.

Last update: 2026-06-01 07:34:45 UTC


README

Automation PHP Version Packagist Downloads PayPal Sponsors via GitHub

Provides a Result type implementation for PHP using ghostwriter/option

Installation

You can install the package via composer:

composer require ghostwriter/result

Usage

use Ghostwriter\Result\Failure;
use Ghostwriter\Result\Success;

// --- Success ---
$success = Success::new('Hello world!');
$success->get(); // 'Hello world!'

// --- Failure ---
$failure = Failure::new(new ExampleException());
$failure->get(); // throws: ResultException
$failure->getOr('Fallback'); // 'Fallback'
$failure->getError(); // returns: instance of ExampleException

// --- Example ---
function divide(int $x, int $y): ResultInterface
{
    if ($y === 0) {
        return Result::failure(new DivisionByZeroError);
    }

    return Result::success($x / $y);
}

divide(1, 0); // Error(DivisionByZeroError)
divide(1, 1); // Success(1)

Credits

Changelog

Please see CHANGELOG.md for more information on what has changed recently.

License

Please see LICENSE for more information on the license that applies to this project.

Security

Please see SECURITY.md for more information on security disclosure process.