42

GitHub - php-defer/php-defer: Golang's defer statement for PHP

 5 years ago
source link: https://github.com/php-defer/php-defer
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

README.md

PHP Defer

Coverage Status Build Status

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

Usage

<?php

defer($context, $callback);

defer requires two parameters: $context and $callback.

  1. $context - unused in your app, required to achieve "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 "...\n";
    });

    defer($_, function () {
        echo "goodbye\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) {
}

// Output:
//
// before exception
// after exception

Credits

This library is inspired by mostka/defer.


Recommend

  • 77

    #1 — Deferred nil funcIf a deferred func evaluates to nil, execution panics when the surrounding func ends not when defer...

  • 62
    • studygolang.com 6 years ago
    • Cache

    golang 中的闭包和defer

    golang中的defer和闭包对很多初学者来说,有时候有很多坑,但是很多介绍的文章有写的乱七八糟.放假了没事可干,就稍微总结一下. 闭包 闭包有叫匿名函数,使用闭包可以使我们的代码更加优雅简洁,顾名思义匿名函数就是没有...

  • 56
    • 微信 mp.weixin.qq.com 6 years ago
    • Cache

    PHP 协程:Go + Chan + Defer

  • 24
    • studygolang.com 6 years ago
    • Cache

    [译] part 29: golang defer

    原文地址: Part 29: Defer 原文作者:

  • 45
    • www.tuicool.com 5 years ago
    • Cache

    golang defer实现原理

    defer是golang提供的关键字,在函数或者方法执行完成,返回之前调用。 每次defer都会将defer函数压入栈中,调用函数或者方法结束时,从栈中取出执行,所以多个defer的执行顺序是先入后出。 defer的触发时机

  • 65
    • Github github.com 5 years ago
    • Cache

    GitHub - php-defer/php-defer at v2.0.0

    PHP Defer A defer statement originally comes from Golang. This library allows you to use defer functionality in PHP code. Usage ...

  • 14
    • studygolang.com 5 years ago
    • Cache

    Golang中的Defer必掌握的7知识点

    在用Golang开发的时候, defer 这个语法也是必备的知识,但是我们除了知道他是在一个函数退出之前执行,对于 defer 是否还有其他地方需要注意的呢。 本文整理的 defer 的全场景使用情...

  • 8

    Golang中defer、return、返回值之间执行顺序的坑 henrylee2cn · 2015-09-13 16:00:00 · 14064 次点击 · 预计阅读时间 2 分钟 · 不到1分钟之前 开始浏览    ...

  • 4

    defer, panic和recover用法【Golang 入门系列十四】 mb5fe94b3e552d9 · 大约10小时之前 · 23 次点击 · 预计阅读时间 3 分钟 · 不到1分钟之前 开始浏览    ...

  • 8

    [Golang] Anonymous Function in Defer Statement May 17, 2018

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK