Download the PHP package petrknap/optional without Composer
On this page you can find all versions of the php package petrknap/optional. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download petrknap/optional
More information about petrknap/optional
Files in petrknap/optional
Package optional
Short Description Optional (like in Java Platform SE 8 but in PHP)
License LGPL-3.0-or-later
Homepage https://github.com/petrknap/php-optional
Informations about the package optional
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 returntrueandget()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) andifPresent()(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.
It is an easy way to make sure that everyone has to check if they have (not) received a null.
Basic usage
Creating your own typed optional
The library provides specialized traits to help you implement strictly typed wrappers with full static analysis support.
Quick Overview
-
OptionalStringclass) OptionalArrayclass)OptionalObjectclass)
Type registration
You can register your custom typed optionals into TypedOptional helper.
Once registered, calling the base Optional class will automatically look up and return the correct specific subclass if the value matches its criteria.
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.