New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

petrknap/optional

Optional (like in Java Platform SE 8 but in PHP)
94,558
Install
composer require petrknap/optional
Latest Version:v3.5.2
PHP:>=8.1
License:LGPL-3.0-or-later
Last Updated:Nov 2, 2026
Links: GitHub  ·  Packagist
Maintainer: petrknap

Optional (like in Java Platform SE but in PHP)

It is an easy way to make sure that everyone has to check if they have (not) received a value.

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 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)

This library delivers complete and reliable behavior of the Optional. The entire feature set is explicitly described and checked by:

This ensures absolute reliability and alignment with the Java specification.

Basic usage

use PetrKnap\Optional\Optional;

// Create string option which holds data
$stringOption = Optional::of('data');
// Print data from string option
echo $stringOption->orElse('missing data');

// Handle function that returns false on failure
Optional::ofFalsable(tmpfile())->ifPresent(function ($tmpFile): void {
    fwrite($tmpFile, 'data');
    fclose($tmpFile);
}, else: fn () => print('tmpfile() failed'));

Creating your own optional

This library provides OptionalOverrides trait to help you implement your own wrapper with full static analysis support (see Some\Optional class):

use PetrKnap\Optional\Optional;
use PetrKnap\Optional\OptionalOverrides;

/**
 * @template-covariant T of mixed type of non-null value
 *
 * @extends Optional<T>
 */
final class MyOptional extends Optional
{
   /** @use OptionalOverrides<T> */
   use OptionalOverrides;
}

⚠ SILENT BREAKING CHANGE ⚠

Optional::equals() Behavior Alignment

The equals() method has been refactored to align strictly with Optional specification.

  1. Comparing two empty Optionals now evaluates to true. Comparing any two empty Optional instances evaluates to true, regardless of their underlying type.
    Optional<String> emptyOptStr = Optional.empty();
    Optional<Array> emptyOptArr = Optional.empty();
    emptyOptStr.equals(emptyOptArr); // true
    
  2. Comparing an Optional against a raw value now evaluates to false. Passing non-optional values directly into equals() will immediately evaluate to false.
    Optional.of("").equals(""); // false
    

Run composer require petrknap/optional to install it. You can support this project via donation. The project is licensed under the terms of the LGPL-3.0-or-later.

Related Packages

sunaoka/laravel-postgres-range

PostgreSQL Range Types for Laravel.

13,963 3
vkovic/laravel-custom-casts

Make your own custom cast type for Laravel model attributes

383,069 217
scrumble-nl/laravel-model-ts-type

This package makes it possible to generate TypeScript types based on your models

99,670 68
filipefernandes/laravel-type-coverage

Laravel code coverage tool for PHPDoc and type declarations

1,246 0
aharisu/generate-form-request-phpdoc

Generate PHPDoc properties automatically based on the rules specification of the...

2,396 1

Version History

Version Released PHP License
v3.5.2 >=8.1 LGPL-3.0-or-later
v3.5.1 >=8.1 LGPL-3.0-or-later
v3.5.0 >=8.1 LGPL-3.0-or-later
v3.4.1 >=8.1 LGPL-3.0-or-later
v3.4.0 >=8.1 LGPL-3.0-or-later
Pre-releases (3)
Version Released PHP License
v4.0.0-rc3 >=8.1 LGPL-3.0-or-later
v4.0.0-rc2 >=8.1 LGPL-3.0-or-later
v4.0.0-rc1 >=8.1 LGPL-3.0-or-later