{"id":13623310,"url":"https://github.com/php-defer/php-defer","last_synced_at":"2026-01-11T16:46:13.174Z","repository":{"id":35153121,"uuid":"213748775","full_name":"php-defer/php-defer","owner":"php-defer","description":"Golang's defer statement for PHP","archived":false,"fork":false,"pushed_at":"2023-09-21T16:40:15.000Z","size":66,"stargazers_count":302,"open_issues_count":0,"forks_count":17,"subscribers_count":8,"default_branch":"5.0","last_synced_at":"2025-04-11T22:16:49.314Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/php-defer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-08T20:36:56.000Z","updated_at":"2025-04-05T10:12:07.000Z","dependencies_parsed_at":"2024-06-18T18:21:38.069Z","dependency_job_id":"5aee1c93-8717-45b6-9949-2dcc038df37d","html_url":"https://github.com/php-defer/php-defer","commit_stats":{"total_commits":25,"total_committers":4,"mean_commits":6.25,"dds":"0.43999999999999995","last_synced_commit":"9f4183d6039e8337fefc3b5e73ed4ede58ee0bf7"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-defer%2Fphp-defer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-defer%2Fphp-defer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-defer%2Fphp-defer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-defer%2Fphp-defer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/php-defer","download_url":"https://codeload.github.com/php-defer/php-defer/tar.gz/refs/heads/5.0","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249089012,"owners_count":21210901,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-08-01T21:01:30.236Z","updated_at":"2026-01-11T16:46:13.121Z","avatar_url":"https://github.com/php-defer.png","language":"PHP","readme":"# PHP Defer\n\n[![Coverage Status](https://coveralls.io/repos/github/php-defer/php-defer/badge.svg?branch=5.0)](https://coveralls.io/github/php-defer/php-defer?branch=5.0)\n[![Tests status](https://github.com/php-defer/php-defer/actions/workflows/tests.yml/badge.svg?branch=5.0)](https://github.com/php-defer/php-defer/actions/workflows/tests.yml?query=branch%3A5.0)\n\nThe [defer statement](https://blog.golang.org/defer-panic-and-recover) originally comes from Golang. This library allows you to use the defer functionality in your PHP code.\n\n## Usage\n\n```php\n\u003c?php\n\ndefer($context, $callback);\n```\n\n`defer` requires two parameters: `$context` and `$callback`.\n\n1. `$context` - unused in your app, required to achieve the \"defer\" effect. I recommend to use `$_` always.\n2. `$callback` - a callback which is executed after the surrounding function returns.\n\n## Examples\n\n### Defer the execution of a code\n\n```php\n\u003c?php\n\nfunction helloGoodbye()\n{\n    defer($_, function () {\n        echo \"goodbye\\n\";\n    });\n\n    defer($_, function () {\n        echo \"...\\n\";\n    });\n\n    echo \"hello\\n\";\n}\n\necho \"before hello\\n\";\nhelloGoodbye();\necho \"after goodbye\\n\";\n\n// Output:\n//\n// before hello\n// hello\n// ...\n// goodbye\n// after goodbye\n```\n\n### Defer and exceptions\n\n```php\n\u003c?php\n\nfunction throwException()\n{\n    defer($_, function () {\n        echo \"after exception\\n\";\n    });\n\n    echo \"before exception\\n\";\n\n    throw new \\Exception('My exception');\n}\n\ntry {\n    throwException();\n} catch (\\Exception $e) {\n    echo \"exception has been caught\\n\";\n}\n\n// Output:\n//\n// before exception\n// after exception\n// exception has been caught\n```\n\n## Installation\n\nPHP Defer supports all PHP versions from `^5.3` to `^8.0`.\nThe following command will install the latest possible version of PHP Defer for your PHP interpreter.\n\n```\ncomposer require \"php-defer/php-defer:^3.0|^4.0|^5.0\"\n```\n\n## Credits\n\nThis library is inspired by [mostka/defer](https://github.com/tito10047/php-defer).\n","funding_links":[],"categories":["目录","Table of Contents","PHP"],"sub_categories":["事件 Event","Event"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-defer%2Fphp-defer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphp-defer%2Fphp-defer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-defer%2Fphp-defer/lists"}