Skip to content

Repository files navigation

PHP Defer

Coverage StatusBuild Status

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

Usage

<?phpdefer($context, $callback);
go_defer($context, $callback);

defer and go_defer require 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.

defer executes callbacks First In, First Out. Functions execute in the order you deferred them.

go_defer more accurately emulates Golang's defer functionality and executes callbacks in Last In, First Out order.

Examples

Defer the execution of a code, using defer

<?phpfunctionhelloGoodbye()
{
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 the execution of a code, using go_defer

<?phpfunctionrollCall()
{
go_defer($_, function () {
echo"I was deferred first!\n";
});
go_defer($_, function () {
echo"I was deferred last!\n";
});
echo"I was NOT deferred!\n";
}
echo"before rollCall\n";
rollCall();
echo"after rollCall\n";
// Output://// before rollCall// I was NOT deferred!// I was deferred last!// I was deferred first!// after rollCall

Defer and exceptions

<?phpfunctionthrowException()
{
defer($_, function () {
echo"after exception\n";
});
echo"before exception\n";
thrownew \Exception('My exception');
}
try {
throwException();
} catch (\Exception$e) {
}
// Output://// before exception// after exception

Credits

This library is inspired by mostka/defer.

About

Golang's defer statement for PHP

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages