承接 php-defer/php-defer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

php-defer/php-defer

最新稳定版本:v5.0.7

Composer 安装命令:

composer require php-defer/php-defer

包简介

Golang's defer statement for PHP

README 文档

README

Coverage Status Tests status

The defer statement originally comes from Golang. This library allows you to use the defer functionality in your PHP code.

Usage

<?php

defer($context, $callback);

defer requires two parameters: $context and $callback.

  1. $context - unused in your app, required to achieve the "defer" effect. I recommend to use $_ always.
  2. $callback - a callback which is executed after the surrounding function returns.

Examples

Defer the execution of a code

<?php

function helloGoodbye()
{
    defer($_, function () {
        echo "goodbye\n";
    });

    defer($_, function () {
        echo "...\n";
    });

    echo "hello\n";
}

echo "before hello\n";
helloGoodbye();
echo "after goodbye\n";

// Output:
//
// before hello
// hello
// ...
// goodbye
// after goodbye

Defer and exceptions

<?php

function throwException()
{
    defer($_, function () {
        echo "after exception\n";
    });

    echo "before exception\n";

    throw new \Exception('My exception');
}

try {
    throwException();
} catch (\Exception $e) {
    echo "exception has been caught\n";
}

// Output:
//
// before exception
// after exception
// exception has been caught

Installation

PHP Defer supports all PHP versions from ^5.3 to ^8.0. The following command will install the latest possible version of PHP Defer for your PHP interpreter.

composer require "php-defer/php-defer:^3.0|^4.0|^5.0"

Credits

This library is inspired by mostka/defer.

统计信息

  • 总下载量: 358.76k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 307
  • 点击次数: 6
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 307
  • Watchers: 8
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04