PHPackages                             samueldmonteiro/result-type - 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. samueldmonteiro/result-type

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

samueldmonteiro/result-type
===========================

Result Type for PHP

1.1.0(1y ago)220MITPHPPHP &gt;=8.2.0

Since Dec 2Pushed 1y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (3)Used By (0)

Result Type
===========

[](#result-type)

[![Maintainer](https://camo.githubusercontent.com/51f615bdefe9b24e707a460205620922c098db3db74bf1210efb68474947368d/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6d61696e7461696e65722d4073616d75656c646d6f6e746569726f2d626c75652e7376673f7374796c653d666c61742d737175617265)](https://www.linkedin.com/in/samuel-m-4a4432250/)[![PHP from Packagist](https://camo.githubusercontent.com/6566b6917a29fcbac20354c932908ba50d4082ed71f55c662cba4f5e400c18ea/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f73616d75656c646d6f6e746569726f2f726573756c742d747970652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/samueldmonteiro/result-type)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Total Downloads](https://camo.githubusercontent.com/72a58c4f150a8343df7adaeac3309359a52fdd84bbe9dc5e2b7d9402c2afdd32/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73616d75656c646d6f6e746569726f2f726573756c742d747970652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/samueldmonteiro/result-type)

###### The result-type library introduces the Result Pattern to PHP, inspired by languages like Rust and Go. It provides a structured way to handle success and error outcomes explicitly, reducing reliance on exceptions. This approach makes your code more predictable, maintainable, and clear, ensuring every operation's result is properly managed.

[](#the-result-type-library-introduces-the-result-pattern-to-php-inspired-by-languages-like-rust-and-go-it-provides-a-structured-way-to-handle-success-and-error-outcomes-explicitly-reducing-reliance-on-exceptions-this-approach-makes-your-code-more-predictable-maintainable-and-clear-ensuring-every-operations-result-is-properly-managed)

A biblioteca result-type traz o padrão Result para o PHP, inspirada em linguagens como Rust e Go. Ela oferece uma maneira estruturada de lidar com sucessos e erros de forma explícita, reduzindo a dependência de exceções. Essa abordagem torna seu código mais previsível, manutenível e claro, garantindo que o resultado de cada operação seja devidamente tratado.

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

[](#installation)

```
composer require samueldmonteiro/result-type
```

Documentation
-------------

[](#documentation)

#### Generics

[](#generics)

###### Since PHP does not yet have native support for generics, we can solve this problem using dockblocks, to define what type of data is being returned when the operation is successful:

[](#since-php-does-not-yet-have-native-support-for-generics-we-can-solve-this-problem-using-dockblocks-to-define-what-type-of-data-is-being-returned-when-the-operation-is-successful)

Como o PHP ainda não tem suporte nativo para generics, podemos resolver esse problema usando dockblocks para definir que tipo de dado será retornado quando a operação for bem-sucedida:

Ex:

```
/**
 * @return Result
 */
function safeDivide(float $value): Result
{
    return $value;
}
```

### Usage

[](#usage)

#### Using Class Instance:

[](#using-class-instance)

```
