PHPackages                             cleancookie/attempt - 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. cleancookie/attempt

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

cleancookie/attempt
===================

Attempt helper function

11PHP

Since Nov 14Pushed 1y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Attempt
=======

[](#attempt)

A simple function to attempt a function inspired by idiomatic error handling in Golang.

Why?
----

[](#why)

I've ran into countless situations where I have to move a variable outside of a try catch just because it is in a different scope

```
// ❌ $success is needed across difference scopes.  Also as an aside $success mutated on different lines
$success = null;
try {
    writeToFile();
    $success = true;
} catch (Exception $e) {
    $success = false;
}

if (!$success) {
    // ...
}
```

```
// ✅ With attempt, I can write code top to bottom, saving on the whiplash
[$error, $value] = attempt(fn() => writeToFile());

$success = !$error;
```

It's also nice to just have one less layer of indentation sometimes

```
// 1 layer of indentation 🙅
try {
    $value = someFunction();
    logger()->debug($value);
} catch (Exception $e) {
    report($e);
}
```

```
// 0 layers of indentation 👌
[$error, $value] = attempt(fn() => someFunction());
```

Installation
------------

[](#installation)

```
composer require cleancookie/attempt
```

And may your functions be attempted

Usage
-----

[](#usage)

```
[$error, $value] = attempt(fn() => throw new Exception('This is an exception'));

if ($error) {
    throw $error; // The reported error will still point to the original exception
}

echo $value;
```

You can also check for specific exception types:

```
[$error, $value] = attempt(fn() => throw new Exception('This is an exception'));

if ($error instanceof \Exception) {
    throw $error;
}

echo $value;
```

You can also ignore errors if you don't care about them:

```
[$_, $value] = attempt(fn() => throw new Exception('This is an exception'));

echo $value;
```

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance30

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity16

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/8b939bee4c411d82c97ed5b263ced9cd9df46f170474285567328a513d61abdf?d=identicon)[Cleancookie](/maintainers/Cleancookie)

---

Top Contributors

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

---

Tags

laravelphp

### Embed Badge

![Health badge](/badges/cleancookie-attempt/health.svg)

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

###  Alternatives

[lokielse/laravel-pinyin

Chinese Pinyin Bundle For Laravel

1613.6k](/packages/lokielse-laravel-pinyin)

PHPackages © 2026

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