PHP code example of serhatozdal / php-java-optional
1. Go to this page and download the library: Download serhatozdal/php-java-optional library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
serhatozdal / php-java-optional example snippets
// 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