PHPackages                             kang-babi/transactions - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. kang-babi/transactions

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

kang-babi/transactions
======================

Gracefully handle an exception from your action.

v1.0.0(1y ago)0117MITPHPPHP ^8.0

Since Apr 15Pushed 1y ago1 watchersCompare

[ Source](https://github.com/kang-babi/transactions)[ Packagist](https://packagist.org/packages/kang-babi/transactions)[ RSS](/packages/kang-babi-transactions/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

Transactions
============

[](#transactions)

The `Transaction` class provides a fluent interface for handling try-catch-finally blocks in a clean and structured way. It allows you to define a `try` block, multiple `catch` blocks for specific exceptions, and a `finally` block that will always execute.

Usage
-----

[](#usage)

### 1. Basic Usage with Try Block

[](#1-basic-usage-with-try-block)

You can define a `try` block to execute your main logic.

```
use KangBabi\Transactions\Transaction;

$result = Transaction::start()
    ->try(fn() => "Hello, World!")
    ->run();

echo $result; // Outputs: Hello, World!
```

### 2. Adding a Catch Block

[](#2-adding-a-catch-block)

You can handle specific exceptions using the catch method.

```
use KangBabi\Transactions\Transaction;

$result = Transaction::start()
    ->try(function () {
        throw new RuntimeException("An error occurred!");
    })
    ->catch(RuntimeException::class, fn($e) => "Caught exception: " . $e->getMessage())
    ->run();

echo $result; // Outputs: Caught exception: An error occurred!
```

### 3. Using a Finally Block

[](#3-using-a-finally-block)

The finally block will always execute, regardless of whether an exception was thrown or not.

```
use KangBabi\Transactions\Transaction;

Transaction::start()
    ->try(function () {
        echo "Trying...\n";
    })
    ->finally(function () {
        echo "Finally block executed.\n";
    })
    ->run();

// Outputs:
// Trying...
// Finally block executed.
```

### 4. Combining Try, Catch, and Finally

[](#4-combining-try-catch-and-finally)

You can combine all three methods for a complete transaction.

```
use KangBabi\Transactions\Transaction;

$result = Transaction::start()
    ->try(function () {
        throw new InvalidArgumentException("Invalid argument!");
    })
    ->catch(InvalidArgumentException::class, fn($e) => "Handled: " . $e->getMessage())
    ->finally(function () {
        echo "Cleaning up resources.\n";
    })
    ->run();

echo $result;
// Outputs:
// Cleaning up resources.
// Handled: Invalid argument!
```

### 5. Handling Multiple Exception Types

[](#5-handling-multiple-exception-types)

You can handle multiple exception types by chaining catch calls.

```
use KangBabi\Transactions\Transaction;

$result = Transaction::start()
    ->try(function () {
        throw new LogicException("Logic error!");
    })
    ->catch(InvalidArgumentException::class, fn($e) => "Caught InvalidArgumentException")
    ->catch(LogicException::class, fn($e) => "Caught LogicException: " . $e->getMessage())
    ->run();

echo $result; // Outputs: Caught LogicException: Logic error!
```

Key Methods
-----------

[](#key-methods)

- `Transaction::start()`: Creates a new transaction instance.
- `try(Closure $try)`: Defines the main logic to execute.
- `catch(string $exception, Closure $catch)`: Handles specific exception types.
- `finally(Closure $finally)`: Defines a block that always executes after the try block.
- `run()`: Executes the transaction and returns the result.

Notes
-----

[](#notes)

- If an exception is thrown and no matching catch block is defined, the exception will propagate.
- The finally block is optional but will always execute if defined.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance47

Moderate activity, may be stable

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

393d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b0387b69f67b51e12f244c06d31bd1d65e08be8879170e7ef168881f5f037d68?d=identicon)[kang-babi](/maintainers/kang-babi)

---

Top Contributors

[![kang-babi](https://avatars.githubusercontent.com/u/81248294?v=4)](https://github.com/kang-babi "kang-babi (9 commits)")

###  Code Quality

TestsPest

Static AnalysisRector

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/kang-babi-transactions/health.svg)

```
[![Health](https://phpackages.com/badges/kang-babi-transactions/health.svg)](https://phpackages.com/packages/kang-babi-transactions)
```

###  Alternatives

[softark/yii2-dual-listbox

Bootstrap Dual Listbox Widget for Yii 2

20144.4k11](/packages/softark-yii2-dual-listbox)[nexylan/nexycrypt

Let's Encrypt ACME protocol PHP client

1065.1k](/packages/nexylan-nexycrypt)

PHPackages © 2026

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