serhatozdal / php-java-optional
Java8 Optional 的完整实现,用于 PHP
1.0
2018-04-14 19:31 UTC
Requires
- php: >=5.5.0
Requires (Dev)
- phpunit/phpunit: ^4
This package is not auto-updated.
Last update: 2024-09-29 05:09:19 UTC
README
Java8 Optional 的完整实现,用于 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