PHPackages                             scope-guard/scope-guard - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. scope-guard/scope-guard

ActiveLibrary[Database &amp; ORM](/categories/database)

scope-guard/scope-guard
=======================

ScopeGuard provides a better way to structure transactions and rollbacks

v1.1.0(5y ago)159PHP

Since Oct 12Pushed 5y ago1 watchersCompare

[ Source](https://github.com/Erikvv/scope-guard-php)[ Packagist](https://packagist.org/packages/scope-guard/scope-guard)[ RSS](/packages/scope-guard-scope-guard/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)DependenciesVersions (3)Used By (0)

```
composer require scope-guard/scope-guard dev-master

```

ScopeGuard for PHP
==================

[](#scopeguard-for-php)

This is a PHP implementation of the ScopeGuard idiom.

You can use this whenever you want to schedule code to run on the end of a scope. If you are already mainly using exceptions the readability of some pieces of code can improve greatly.

You can differentiate between code that should be executed

- always (onExit)
- when the scope is left early through an exception or early return (onFailure)
- when the scope is left sucessfully (onSuccess)

Simplest example
----------------

[](#simplest-example)

This closes a connection on scope exit.

```
use ScopeGuard\Scope;

$connection = connect()
$scope = new Scope;
$scope->onExit([$connection, 'disconnect']);

/* do stuff with $connection */

/* explicit or implicit return, ->disconnect() is called here */
return;
```

OnFailure example
-----------------

[](#onfailure-example)

```
use ScopeGuard\Scope;

$salesEntry = new SalesEntry;

$scope = new Scope;
$scope->onFailure(function use ($serviceDesk, $salesEntry) { $serviceDesk->alertRepresentative($salesEntry); });

/* more logic here */

// this makes sure any success handlers are called.
// any other path leads to the failure handlers being called
$scope->markSuccessful();

// unset can manually trigger the handlers (taking into account the regular reference-counting semantics)
unset($scope);
```

Probably you will find onFailure the most useful for doing rollbacks, attaching more handlers as the script progresses, and entering the failure flow by throwing an exception.

OnSuccess will generally not be used because your normal code is the success case, but it may help to group some statements more logically.

OnExit can be used for something like cleanup which must be done regardless of what the eventual outcome of the script was.

It is perfectly fine to use multiple Scope object at the same time.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1356 days

Total

2

Last Release

2149d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4174d83c59a421839273c1ae855033d0fae26e6ca953509fc3f7c984640af51b?d=identicon)[Erikvv](/maintainers/Erikvv)

---

Top Contributors

[![Erikvv](https://avatars.githubusercontent.com/u/1748197?v=4)](https://github.com/Erikvv "Erikvv (13 commits)")

### Embed Badge

![Health badge](/badges/scope-guard-scope-guard/health.svg)

```
[![Health](https://phpackages.com/badges/scope-guard-scope-guard/health.svg)](https://phpackages.com/packages/scope-guard-scope-guard)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
