https://github.com/petrknap/php-optional
Optional (like in Java Platform SE 8 but in PHP)
https://github.com/petrknap/php-optional
datatype optional php php-library type
Last synced: 2 months ago
JSON representation
Optional (like in Java Platform SE 8 but in PHP)
- Host: GitHub
- URL: https://github.com/petrknap/php-optional
- Owner: petrknap
- License: lgpl-3.0
- Created: 2024-05-10T19:54:44.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2026-01-10T09:42:32.000Z (5 months ago)
- Last Synced: 2026-01-11T02:42:30.245Z (5 months ago)
- Topics: datatype, optional, php, php-library, type
- Language: PHP
- Homepage:
- Size: 104 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yaml
- License: COPYING
- Authors: AUTHORS
Awesome Lists containing this project
README
# Optional (like in Java Platform SE 8 but in PHP)
> A container object which may or may not contain a non-null value. If a value is present, `isPresent()` will return `true` and `get()` will return the value.
>
> Additional methods that depend on the presence or absence of a contained value are provided, such as `orElse()` (return a default value if value not present) and `ifPresent()` (execute a block of code if the value is present).
>
> This is a [value-based](https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html) class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of Optional may have unpredictable results and should be avoided.
>
> --
> [Optional (Java Platform SE 8)](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html)
It is an easy way to make sure that everyone has to check if they have (not) received a `null`.
## Examples
```php
namespace PetrKnap\Optional;
/** @var Optional $optionalString */
$optionalString = Optional::of('data');
if ($optionalString->isPresent()) {
echo $optionalString->get();
}
OptionalResource::ofFalsable(tmpfile())->ifPresent(function ($tmpFile): void {
fwrite($tmpFile, 'data');
fclose($tmpFile);
}, else: fn () => print('tmpfile() failed'));
```
### Create and use your own typed optional
```php
namespace PetrKnap\Optional;
/**
* @extends OptionalObject
*/
class YourOptional extends OptionalObject {
protected static function getInstanceOf(): string {
return Some\DataObject::class;
}
}
TypedOptional::register(YourOptional::class); // optional recommended step
function your_strong_typed_function(YourOptional $input): YourOptional {
return YourOptional::empty();
}
/**
* @param Optional $input
*
* @return Optional
*/
function your_weak_typed_function(Optional $input): Optional {
return YourOptional::empty();
}
```
---
Run `composer require petrknap/optional` to install it.
You can [support this project via donation](https://petrknap.github.io/donate.html).
The project is licensed under [the terms of the `LGPL-3.0-or-later`](./COPYING.LESSER).