https://github.com/ghostwriter/result
Provides a Result type implementation for PHP
https://github.com/ghostwriter/result
ghostwriter php result result-type
Last synced: 28 days ago
JSON representation
Provides a Result type implementation for PHP
- Host: GitHub
- URL: https://github.com/ghostwriter/result
- Owner: ghostwriter
- License: other
- Created: 2021-11-23T01:29:41.000Z (over 3 years ago)
- Default Branch: 2.0.x
- Last Pushed: 2025-04-22T00:52:04.000Z (29 days ago)
- Last Synced: 2025-04-22T01:30:32.478Z (29 days ago)
- Topics: ghostwriter, php, result, result-type
- Language: PHP
- Homepage: https://github.com/ghostwriter/result
- Size: 764 KB
- Stars: 0
- 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.