phplang/scope-exit

Emulation of SCOPE_EXIT construct from C++

Maintainers

👁 sgolemon

Package info

github.com/phplang/scope-exit

pkg:composer/phplang/scope-exit

Statistics

Installs: 13 233 056

Dependents: 4

Suggesters: 0

Stars: 19

Open Issues: 1

1.0.0 2016-09-17 00:15 UTC

Requires

None

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

BSD 239b73abe89f9414aa85a7ca075ec9445629192b

cleanupscopeexit

This package is not auto-updated.

Last update: 2026-06-07 05:28:39 UTC


README

This simple class provides an implementation of C++'s SCOPE_EXIT, or GoLang's defer.

To use, assign an instance of this object to a local variable. When that variable falls out of scope (or is explicitly unset), the callback passed to the constructor will be invoked. This is useful, for example, to aid cleanup at the end of a function.

function f(&$x) {
 $x = 1;
 $_ = new \PhpLang\ScopeExit(function() use (&$x) { $x = 2; });
 // $x is still 1 at this point.
 return 42;
 // After the return, the local scope is cleaned up, the closure is invoked, and it's set to 2
}

f($a);
var_dump($a); // int(2)