PHPackages                             liamja/defer - 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. liamja/defer

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

liamja/defer
============

06PHP

Since Feb 18Pushed 8y ago1 watchersCompare

[ Source](https://github.com/liamja/defer)[ Packagist](https://packagist.org/packages/liamja/defer)[ RSS](/packages/liamja-defer/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Defer
=====

[](#defer)

Postpone the calling of a function or callable.

Why?
----

[](#why)

From *Go by Example*:

> Defer is used to ensure that a function call is performed later in a program’s execution, usually for purposes of cleanup. `defer` is often used where e.g. `ensure` and `finally` would be used in other languages.

Common use cases are:

- Cleaning up temporary files.
- Closing network connections.
- Closing database connections.

Comparing `defer` to `finally`, this implementation of `defer` will allow us to have better control over when our deferred functions are called; we can decide when to start stacking deferred functions, and where to finally call them.

Examples
--------

[](#examples)

### Usage

[](#usage)

```
// Create an instance of Defer.
// When $defer falls out of scope, the deferred callables will be called in reverse order.
$defer = new Defer;

// Push your deferred tasks to the $defer object.
$defer->push(function () {
    echo "I'm echoed last!";
});

// As a convenience, you can also call $defer as a function
$defer(function () {
    echo "I'm echoed second!";
});

echo "I'm called first!";
```

### Closing Resources

[](#closing-resources)

Defer can be used for ensuring the closing of open files:

```
$fp = fopen('/tmp/file', 'w');

$defer(function () use ($fp) {
   fclose($fp);
});

fwrite($fp, 'Some temporary data.');
```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/37d06715b6487a154314e32970d1a781e53a6f686de2430dcf0412f3f6db4afe?d=identicon)[liamja](/maintainers/liamja)

---

Top Contributors

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

---

Tags

composer-librarycomposer-packagephpphp5php53php54php55php56

### Embed Badge

![Health badge](/badges/liamja-defer/health.svg)

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

PHPackages © 2026

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