Full implementation of JAVA8 Optional for PHP
// 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
composer require serhatozdal/php-java-optional:1.0