https://github.com/ghostwriter/result
Provides a Result type implementation for PHP
https://github.com/ghostwriter/result
ghostwriter php result result-type
Last synced: 3 months ago
JSON representation
Provides a Result type implementation for PHP
- Host: GitHub
- URL: https://github.com/ghostwriter/result
- Owner: ghostwriter
- License: bsd-3-clause
- Created: 2021-11-23T01:29:41.000Z (over 4 years ago)
- Default Branch: 2.0.x
- Last Pushed: 2026-03-17T04:44:56.000Z (3 months ago)
- Last Synced: 2026-03-17T19:32:29.218Z (3 months ago)
- Topics: ghostwriter, php, result, result-type
- Language: PHP
- Homepage: https://github.com/ghostwriter/result
- Size: 62.4 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
# Result
[](https://github.com/sponsors/ghostwriter)
[](https://github.com/ghostwriter/result/actions/workflows/automation.yml)
[](https://www.php.net/supported-versions)
[](https://packagist.org/packages/ghostwriter/result)
Provides a **`Result`** type implementation for PHP using [`ghostwriter/option`](https://github.com/ghostwriter/option)
## Installation
You can install the package via composer:
``` bash
composer require ghostwriter/result
```
## Usage
```php
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
- [Nathanael Esayeas](https://github.com/ghostwriter)
- [All Contributors](https://github.com/ghostwriter/result/contributors)
### Changelog
Please see [CHANGELOG.md](./CHANGELOG.md) for more information on what has changed recently.
### License
Please see [LICENSE](./LICENSE) for more information on the license that applies to this project.
### Security
Please see [SECURITY.md](./SECURITY.md) for more information on security disclosure process.