serhatozdal / php-java-optional
Full implementation of Java8 Optional for PHP
Installs: 1 159
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >=5.5.0
Requires (Dev)
- phpunit/phpunit: ^4
This package is not auto-updated.
Last update: 2025-05-25 09:14:21 UTC
README
Full implementation of JAVA8 Optional for PHP
Usage
// ofEmpty refers Optional#empty() in java // It is renamed as ofEmpty() because of empty() is reserved by PHP Optional::ofEmpty()->isPresent(); // false Optional::of("value")->orElse("elseValue"); // value Optional::ofEmpty()->orElseThrow(function () { throw .... }); // throws exception Optional::ofEmpty()->filter(function ($a) { return (int) $a; }); // function is not executed Optional::of(5)->map(function ($a) { return $a * 2; })->get(); // returns 10 Optional::ofEmpty()->orElseGet(function () { return 10; }); // returns 10
Installation
composer require serhatozdal/php-java-optional:1.0