Golang's defer statement for PHP

Related tags

Event php-defer
Overview

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 "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 ^7.4. 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.

You might also like...
 PHP Application using DDD CQRS Event Sourcing with Hexagonal Architecture
PHP Application using DDD CQRS Event Sourcing with Hexagonal Architecture

PHP Application using DDD CQRS Event Sourcing with Hexagonal Architecture Application built with Ecotone Framework and powered by integrations with Pr

Revolt is a rock-solid event loop for concurrent PHP applications.

Revolt is a rock-solid event loop for concurrent PHP applications.

Get MYSQL statement from query builder in laravel helper

Get MYSQL statement laravel This package allows to get mysql statement that query builder in laravel made it for debugging purposes. Basic usage Dump

Laravel Eloquent CASE Statement Support

This packages adds CASE statement support to Laravel Query Builder. It supports Laravel 8.x & 9.x.

This project is based on the problem statement provided by the Minstry of HRD (India) for Smart India Hackathon '17.
This project is based on the problem statement provided by the Minstry of HRD (India) for Smart India Hackathon '17.

This project is based on the problem statement provided by the Minstry of HRD (India) for Smart India Hackathon '17. As per the given problem statement, we need to solve the problem of bunching of marks at certain level, and problem of high scorers being at disadvantageous position due to lower competitive percentile.

PHPMD is a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD. PHPMD can be seen as an user friendly frontend application for the raw metrics stream measured by PHP Depend.

PHPMD PHPMD is a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD. PHPMD can be seen as an user friendly

The Current US Version of PHP-Nuke Evolution Xtreme v3.0.1b-beta often known as Nuke-Evolution Xtreme. This is a hardened version of PHP-Nuke and is secure and safe. We are currently porting Xtreme over to PHP 8.0.3
The Current US Version of PHP-Nuke Evolution Xtreme v3.0.1b-beta often known as Nuke-Evolution Xtreme. This is a hardened version of PHP-Nuke and is secure and safe. We are currently porting Xtreme over to PHP 8.0.3

2021 Nightly Builds Repository PHP-Nuke Evolution Xtreme Developers TheGhost - Ernest Allen Buffington (Lead Developer) SeaBeast08 - Sebastian Scott B

PHP Kafka client is used in PHP-FPM and Swoole. PHP Kafka client supports 50 APIs, which might be one that supports the most message types ever.

longlang/phpkafka Introduction English | 简体中文 PHP Kafka client is used in PHP-FPM and Swoole. The communication protocol is based on the JSON file in

A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM.

A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM. s('string')-toTitleCase()-ensureRight('y') ==

A sampling profiler for PHP written in PHP, which reads information about running PHP VM from outside of the process.

Reli Reli is a sampling profiler (or a VM state inspector) written in PHP. It can read information about running PHP script from outside of the proces

PHP Meminfo is a PHP extension that gives you insights on the PHP memory content

MEMINFO PHP Meminfo is a PHP extension that gives you insights on the PHP memory content. Its main goal is to help you understand memory leaks: by loo

A sampling profiler for PHP written in PHP, which reads information about running PHP VM from outside of the process.

Reli Reli is a sampling profiler (or a VM state inspector) written in PHP. It can read information about running PHP script from outside of the proces

GitHub action to setup PHP with required extensions, php.ini configuration, code-coverage support and various tools like composer...
GitHub action to setup PHP with required extensions, php.ini configuration, code-coverage support and various tools like composer...

Setup PHP in GitHub Actions Setup PHP with required extensions, php.ini configuration, code-coverage support and various tools like composer in GitHub

A PHP parser written in PHP

PHP Parser This is a PHP 5.2 to PHP 8.0 parser written in PHP. Its purpose is to simplify static code analysis and manipulation. Documentation for ver

A PHP VM implementation in PHP
A PHP VM implementation in PHP

PHPPHP A PHP VM implementation written in PHP. This is a basic VM implemented in PHP using the AST generating parser developed by @nikic To see what's

PHP Censor is an open source self-hosted continuous integration server for PHP projects.
PHP Censor is an open source self-hosted continuous integration server for PHP projects.

PHP Censor is an open source, self-hosted, continuous integration server for PHP projects (PHPCI fork). Official twitter @php_censor. PHP Censor versi

PHP Integrated Query, a real LINQ library for PHP

PHP Integrated Query - Official site What is PINQ? Based off the .NET's LINQ (Language integrated query), PINQ unifies querying across arrays/iterator

Handle PHP errors, dump variables, execute PHP code remotely in Google Chrome

PHP Console server library PHP Console allows you to handle PHP errors & exceptions, dump variables, execute PHP code remotely and many other things u

PHP APM (Alternative PHP Monitor)

APM (Alternative PHP Monitor) APM (Alternative PHP Monitor) is a monitoring extension enabling native Application Performance Management (APM) for PHP

Comments
  • Feature - LIFO Execution Order (Matching Go's Behavior)

    Feature - LIFO Execution Order (Matching Go's Behavior)

    Scope

    This PR is a re-implementation of #2, with the differences being that:

    • It changes the default implementation (rather than providing an alternative) to match Go's behavior of LIFO execution order.
    • It uses the built-in SplStack data-structure for the internal stacking context implementation.

    Credit

    Credit goes to @francislavoie for this idea, in which this implementation was based off-of! He even encouraged me to contribute via my own PR (https://github.com/php-defer/php-defer/pull/2#issuecomment-540227965). Thanks!

    opened by Rican7 8
  • Implement rdefer which runs in reverse order, matching Go's behaviour

    Implement rdefer which runs in reverse order, matching Go's behaviour

    Add an alternate implementation which runs in reverse order of the current implementation, which is more correct to how Go executes it.

    I mainly just wrote this as a proof-of-concept, maybe this lib doesn't need this. But maybe you'd want to replace the existing implementation with this one? Up to you.

    opened by francislavoie 8
  • Feature - Add `go_defer` function to mirror Golang's LIFO defer behavior.

    Feature - Add `go_defer` function to mirror Golang's LIFO defer behavior.

    Similar to the implementation of #3, and the idea of providing an alternative instead of a replacement in #2. Implementation was drafted prior to my knowledge of these existing PR's.

    This implementation in no way alters or changes the original defer function provided. This was mostly an academic exercise rather than an effort to assert a correct ordering or to specifically match Golang's specification.

    My derivative is an optional second function, go_defer, to allow the proper FIFO/LIFO implementation choice up to the user. It comes with its own rewritten copy of the Tests, and an updated README to explain and accommodate examples for both functions.

    opened by jpmc 2
Owner
null
Event-driven, non-blocking I/O with PHP.

Event-driven, non-blocking I/O with PHP. ReactPHP is a low-level library for event-driven programming in PHP. At its core is an event loop, on top of

ReactPHP 8.5k Jan 8, 2023
A non-blocking concurrency framework for PHP applications. 🐘

Amp is a non-blocking concurrency framework for PHP. It provides an event loop, promises and streams as a base for asynchronous programming. Promises

Amp 3.8k Jan 6, 2023
Icicle is a PHP library for writing asynchronous code using synchronous coding techniques

Icicle is now deprecated in favor of Amp v2.0. This version is is currently under development, but close to release. The v2.0 branches are amp_v2 in a

icicle.io 1.1k Dec 21, 2022
Événement is a very simple event dispatching library for PHP.

Événement Événement is a very simple event dispatching library for PHP. It has the same design goals as Silex and Pimple, to empower the user while st

Igor 1.2k Jan 4, 2023
Asynchronous coroutines for PHP 7.

Recoil An asynchronous coroutine kernel for PHP 7. composer require recoil/recoil The Recoil project comprises the following packages: recoil/api - T

Recoil 787 Dec 8, 2022
A pragmatic event sourcing library for PHP with a focus on developer experience.

EventSaucePHP EventSauce is a somewhat opinionated, no-nonsense, and easy way to introduce event sourcing into PHP projects. It's designed so storage

EventSauce 685 Dec 31, 2022
PHP 7.4 EventStore Implementation

Prooph Event Store Common classes and interface for Prooph Event Store implementations. Installation You can install prooph/event-store via composer b

null 532 Dec 9, 2022
Reactive extensions for PHP.

This package is abandoned. Use https://github.com/ReactiveX/RxPHP instead Rx.PHP Reactive extensions for PHP. The reactive extensions for PHP are a se

Alexander 208 Apr 6, 2021
Reactive extensions for PHP

RxPHP Reactive extensions for PHP. The reactive extensions for PHP are a set of libraries to compose asynchronous and event-based programs using obser

ReactiveX 1.6k Dec 12, 2022
🚀 Coroutine-based concurrency library for PHP

English | 中文 Swoole is an event-driven asynchronous & coroutine-based concurrency networking communication engine with high performance written in C++

Swoole Project 17.7k Jan 5, 2023