{"id":21194148,"url":"https://github.com/petrknap/php-optional","last_synced_at":"2026-04-07T05:31:34.130Z","repository":{"id":239233112,"uuid":"798938343","full_name":"petrknap/php-optional","owner":"petrknap","description":"Optional (like in Java Platform SE 8 but in PHP)","archived":false,"fork":false,"pushed_at":"2026-01-10T09:42:32.000Z","size":107,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-11T02:42:30.245Z","etag":null,"topics":["datatype","optional","php","php-library","type"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/petrknap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yaml","license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"custom":"https://petrknap.github.io/donate.html"}},"created_at":"2024-05-10T19:54:44.000Z","updated_at":"2026-01-10T08:35:10.000Z","dependencies_parsed_at":"2024-05-28T19:09:01.363Z","dependency_job_id":"2685069f-4d8b-45d5-b2a8-a034cc010a1f","html_url":"https://github.com/petrknap/php-optional","commit_stats":null,"previous_names":["petrknap/php-optional"],"tags_count":30,"template":false,"template_full_name":null,"purl":"pkg:github/petrknap/php-optional","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-optional","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-optional/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-optional/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-optional/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petrknap","download_url":"https://codeload.github.com/petrknap/php-optional/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-optional/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31501903,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-07T03:10:19.677Z","status":"ssl_error","status_checked_at":"2026-04-07T03:10:13.982Z","response_time":105,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["datatype","optional","php","php-library","type"],"created_at":"2024-11-20T19:19:51.372Z","updated_at":"2026-04-07T05:31:34.105Z","avatar_url":"https://github.com/petrknap.png","language":"PHP","funding_links":["https://petrknap.github.io/donate.html"],"categories":[],"sub_categories":[],"readme":"# Optional (like in Java Platform SE 8 but in PHP)\n\n\u003e A container object which may or may not contain a non-null value. If a value is present, `isPresent()` will return `true` and `get()` will return the value.\n\u003e\n\u003e 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) and `ifPresent()` (execute a block of code if the value is present).\n\u003e\n\u003e This is a [value-based](https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html) 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.\n\u003e\n\u003e --\n\u003e [Optional (Java Platform SE 8)](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html)\n\nIt is an easy way to make sure that everyone has to check if they have (not) received a `null`.\n\n## Examples\n\n```php\nnamespace PetrKnap\\Optional;\n\n/** @var Optional\u003cstring\u003e $optionalString */\n$optionalString = Optional::of('data');\nif ($optionalString-\u003eisPresent()) {\n    echo $optionalString-\u003eget();\n}\n\nOptionalResource::ofFalsable(tmpfile())-\u003eifPresent(function ($tmpFile): void {\n    fwrite($tmpFile, 'data');\n    fclose($tmpFile);\n}, else: fn () =\u003e print('tmpfile() failed'));\n```\n\n### Create and use your own typed optional\n\n```php\nnamespace PetrKnap\\Optional;\n\n/**\n * @extends OptionalObject\u003cSome\\DataObject\u003e\n */\nclass YourOptional extends OptionalObject {\n    protected static function getInstanceOf(): string {\n        return Some\\DataObject::class;\n    }\n}\nTypedOptional::register(YourOptional::class); // optional recommended step\n\nfunction your_strong_typed_function(YourOptional $input): YourOptional {\n    return YourOptional::empty();\n}\n\n/**\n * @param Optional\u003cSome\\DataObject\u003e $input\n *\n * @return Optional\u003cSome\\DataObject\u003e\n */\nfunction your_weak_typed_function(Optional $input): Optional {\n    return YourOptional::empty();\n}\n```\n\n---\n\nRun `composer require petrknap/optional` to install it.\nYou can [support this project via donation](https://petrknap.github.io/donate.html).\nThe project is licensed under [the terms of the `LGPL-3.0-or-later`](./COPYING.LESSER).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetrknap%2Fphp-optional","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetrknap%2Fphp-optional","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetrknap%2Fphp-optional/lists"}