https://github.com/yceruto/option-type
The Option type for PHP
https://github.com/yceruto/option-type
null-safety option-type optional-type php
Last synced: 11 months ago
JSON representation
The Option type for PHP
- Host: GitHub
- URL: https://github.com/yceruto/option-type
- Owner: yceruto
- License: mit
- Created: 2024-04-18T12:49:59.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-27T13:18:42.000Z (over 1 year ago)
- Last Synced: 2025-07-06T00:33:07.750Z (12 months ago)
- Topics: null-safety, option-type, optional-type, php
- Language: PHP
- Homepage:
- Size: 67.4 KB
- Stars: 15
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# PHP Option type
[](https://packagist.org/packages/yceruto/option-type)
[](https://packagist.org/packages/yceruto/option-type)
[](https://packagist.org/packages/yceruto/option-type)
[](https://packagist.org/packages/yceruto/option-type)
The `Option` type represents a value that might or might not be there. It's all about
null safety in PHP!
> [!NOTE]
> Inspired by [Rust's Option type](https://doc.rust-lang.org/std/option/) and other
> languages like Scala, Swift, F#, etc.
## Installation
```bash
composer require yceruto/option-type
```
## Handling the presence or absence of a value with `null`
In PHP, denoting the absence of a value is done with `null`, e.g. when a `divide`
function returns `null` if the divisor is `0`.
```php
function divide(int $dividend, int $divisor): ?int
{
if (0 === $divisor) {
return null;
}
return intdiv($dividend, $divisor);
}
function success(int $result): string {
return sprintf('Result: %d', $result);
}
$result = divide(10, 2);
echo success($result);
```
Can you spot the issue in this code? Apparently, everything is fine until you try to
divide by zero. The function will return `null`, and the `success()` function will throw
a `TypeError` because it expects an `int` value, not `null`.
The issue with this approach is that it's too easy to overlook checking if the value is
`null`, leading to runtime errors, and this is where the `Option` type comes in handy: it
always forces you to deal with the `null` case.
## Handling the presence or absence of a value with `Option`
Options often work with pattern matching to check if there’s a value and act accordingly,
always making sure to handle the `null` case.
```php
use Std\Type\Option;
use function Std\Type\Option\none;
use function Std\Type\Option\some;
/**
* @return Option
*/
function divide(int $dividend, int $divisor): Option
{
if (0 === $divisor) {
return none();
}
return some(intdiv($dividend, $divisor));
}
function success(int $result): string {
return sprintf('Result: %d', $result);
}
// The return value of the function is an Option
$result = divide(10, 2);
// Pattern match to retrieve the value
echo $result->match(
// The division was valid
some: fn (int $v) => success($v),
// The division was invalid
none: fn () => 'Division by zero!',
);
```
> [!TIP]
>You can use the functions `some()` and `none()` as quick ways to create an `Option`
>instance. `some()` is just like `new Some()`, meaning it includes a value, while
>`none()` is the same as `new None()`, indicating it is missing a value.
## Documentation
* [API Reference](docs/api_reference.md)
* [Examples](docs/examples.md)
## License
This software is published under the [MIT License](LICENSE)