PHPackages                             gielfeldt/shutdownhandler - 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. gielfeldt/shutdownhandler

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

gielfeldt/shutdownhandler
=========================

Shutdown Handler.

1.2.0(10y ago)1771MITPHPPHP &gt;=5.3.0

Since Aug 7Pushed 10y ago1 watchersCompare

[ Source](https://github.com/gielfeldt/shutdownhandler)[ Packagist](https://packagist.org/packages/gielfeldt/shutdownhandler)[ Docs](https://github.com/gielfeldt/shutdownhandler)[ RSS](/packages/gielfeldt-shutdownhandler/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (6)Used By (0)

Shutdown Handler
================

[](#shutdown-handler)

[![Build Status](https://camo.githubusercontent.com/602ca3cd8d95f14265567a006fca605aea3eb2f0387cb2c23ef3eb51418a2c6e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6769656c66656c64742f73687574646f776e68616e646c65722f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/gielfeldt/shutdownhandler/build-status/master)[![Test Coverage](https://camo.githubusercontent.com/661cff7ec9bc7f5b1a9cacc5c9e64cfeba227086fdb6d7feba78989439d181c3/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6769656c66656c64742f73687574646f776e68616e646c65722f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/gielfeldt/shutdownhandler/coverage)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/3e643392e3b0d2ff30b5f8402b4f2d2b52b11bbea740aa753302baaf5383e06f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6769656c66656c64742f73687574646f776e68616e646c65722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/gielfeldt/shutdownhandler/?branch=master)[![Code Climate](https://camo.githubusercontent.com/ee2b54e50856426881abcd7bb562a5a89e69e8ed7e2394fe02ea32094dea1396/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6769656c66656c64742f73687574646f776e68616e646c65722f6261646765732f6770612e737667)](https://codeclimate.com/github/gielfeldt/shutdownhandler)

[![Latest Stable Version](https://camo.githubusercontent.com/a2a9f5c2d0581d0f040548d6dc331c1f0d2442ac643cf761cdaf28adfc0d38f7/68747470733a2f2f706f7365722e707567782e6f72672f6769656c66656c64742f73687574646f776e68616e646c65722f762f737461626c652e737667)](https://packagist.org/packages/gielfeldt/shutdownhandler)[![Latest Unstable Version](https://camo.githubusercontent.com/fc0a1d234d4e211acd255e99a058c6aedb3e1270534ebc9d6c5da006f47b717b/68747470733a2f2f706f7365722e707567782e6f72672f6769656c66656c64742f73687574646f776e68616e646c65722f762f756e737461626c652e737667)](https://packagist.org/packages/gielfeldt/shutdownhandler)[![Dependency Status](https://camo.githubusercontent.com/a9dd47c54a1bcdbb760d3a890e224f3cb5e878481c96d56bf3c1e68e5bf06a89/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3535636232656239646665643061303031653030303230302f62616467652e7376673f7374796c653d666c6174)](https://www.versioneye.com/user/projects/55cb2eb9dfed0a001e000200)[![License](https://camo.githubusercontent.com/929154cedfc0b4e0908724d94df3776048c9370f174db4a62cb1a45d7c015f1b/68747470733a2f2f706f7365722e707567782e6f72672f6769656c66656c64742f73687574646f776e68616e646c65722f6c6963656e73652e737667)](https://github.com/gielfeldt/shutdownhandler/blob/master/LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/b0fda24c4254d6b3ca3c2fce39baa96189fae53fbcf63a91743c98930b58eac3/68747470733a2f2f706f7365722e707567782e6f72672f6769656c66656c64742f73687574646f776e68616e646c65722f646f776e6c6f6164732e737667)](https://packagist.org/packages/gielfeldt/shutdownhandler)

[![Documentation Status](https://camo.githubusercontent.com/382440bcd9e353a0c298f3b51067d2ad80ca9af68c764ce9b3d754f354a63b38/68747470733a2f2f72656164746865646f63732e6f72672f70726f6a656374732f73687574646f776e68616e646c65722f62616467652f3f76657273696f6e3d737461626c65)](https://readthedocs.org/projects/shutdownhandler/?badge=latest)[![Documentation Status](https://camo.githubusercontent.com/0b77f550804a484547a5d4ada2be025eba8d30f42d5b58e9cae91f3e7f4a8506/68747470733a2f2f72656164746865646f63732e6f72672f70726f6a656374732f73687574646f776e68616e646c65722f62616467652f3f76657273696f6e3d6c6174657374)](https://readthedocs.org/projects/shutdownhandler/?badge=latest)

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

[](#installation)

To install the ShutdownHandler library in your project using Composer, first add the following to your `composer.json`config file.

```
{
    "require": {
        "gielfeldt/shutdownhandler": "~1.0"
    }
}
```

Then run Composer's install or update commands to complete installation. Please visit the [Composer homepage](http://getcomposer.org) for more information about how to use Composer.

### Shutdown handler

[](#shutdown-handler-1)

This shutdown handler class allows you to create advanced shutdown handlers, that can be manipulated after being created.

#### Motivation

[](#motivation)

1. Destructors are not run on fatal errors. In my particular case, I needed a lock class that was robust wrt to cleaning up after itself. See "Example 2" below or examples/fatal.php for an example of this.
2. PHP shutdown handlers cannot be manipulated after registration (unregister, execute, etc.).

#### Example 1 - using Shutdown Handler

[](#example-1---using-shutdown-handler)

```
namespace Gielfeldt\ShutdownHandler\Example;

require 'vendor/autoload.php';

use Gielfeldt\ShutdownHandler\ShutdownHandler;

/**
 * Simple shutdown handler callback.
 *
 * @param string $message
 *   Message to display during shutdown.
 */
function myshutdownhandler($message = '')
{
    echo "Goodbye $message\n";
}

// Register shutdown handler to be run during PHP shutdown phase.
$handler = new ShutdownHandler('\Gielfeldt\ShutdownHandler\Example\myshutdownhandler', array('cruel world'));

echo "Hello world\n";

// Register shutdown handler.
$handler2 = new ShutdownHandler('\Gielfeldt\ShutdownHandler\Example\myshutdownhandler', array('for now'));

// Don't wait for shutdown phase, just run now.
$handler2->run();
```

#### Example 2 - Ensuring object destruction

[](#example-2---ensuring-object-destruction)

```
namespace Gielfeldt\ShutdownHandler\Example;

require 'vendor/autoload.php';

use Gielfeldt\ShutdownHandler\ShutdownHandler;

/**
 * Test class with destructor via Gielfeldt\ShutdownHandler\ShutdownHandler.
 */
class MyClass
{
    /**
     * Reference to the shutdown handler object.
     * @var ShutdownHandler
     */
    protected $shutdown;

    /**
     * Constructor.
     *
     * @param string $message
     *   Message to display during destruction.
     */
    public function __construct($message = '')
    {
        // Register our shutdown handler.
        $this->shutdown = new ShutdownHandler(array(get_class($this), 'shutdown'), array($message));
    }

    /**
     * Run our shutdown handler upon object destruction.
     */
    public function __destruct()
    {
        $this->shutdown->run();
    }

    /**
     * Our shutdown handler.
     *
     * @param string $message
     *   The message to display.
     */
    public static function shutdown($message = '')
    {
        echo "Destroy $message\n";
    }
}

// Instantiate object.
$obj = new MyClass("world");

// Destroy object. The object's shutdown handler will be run.
unset($obj);

// Instantiate new object.
$obj = new MyClass("universe");

// Object's shutdown handler will be run on object's destruction or when PHP's
// shutdown handlers are executed. Whichever comes first.
```

For more examples see the examples/ folder.

#### Features

[](#features)

- Unregister a shutdown handler
- Run a shutdown handler prematurely
- Improve object destructors by ensuring destruction via PHP shutdown handlers
- Keyed shutdown handlers, allowing to easily deduplicate multiple shutdown handlers

#### Caveats

[](#caveats)

1. Lots probably.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity62

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 ~22 days

Total

3

Last Release

3885d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e93f60ea3dd735998209863ed3e064b4aa440db3c23b08f44bf0655c28b0bd3c?d=identicon)[gielfeldt](/maintainers/gielfeldt)

---

Top Contributors

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

---

Tags

handlershutdowndestructor

### Embed Badge

![Health badge](/badges/gielfeldt-shutdownhandler/health.svg)

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

PHPackages © 2026

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