Description
A defer statement originally comes from Golang. This library allows you to use defer functionality in PHP code.
PHP Defer alternatives and similar libraries
Based on the "Event" category.
Alternatively, view PHP Defer alternatives based on common mentions on social networks and blogs.
-
Workerman
An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols. -
Cake Event
[READ-ONLY] The event dispatcher library for CakePHP. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp
CodeRabbit: AI Code Reviews for Developers
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of PHP Defer or a related project?
Popular Comparisons
README
PHP Defer
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
.
$context
- unused in your app, required to achieve "defer" effect. I recommend to use$_
always.$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.