Search by

texthtml / object-reaper

mathroc

Be notified of objects being destroyed

Package info

github.com/texthtml/php-object-reaper

Language:Shell

pkg:composer/texthtml/object-reaper

Statistics

Installs: 1 095

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 1

v1.2.0 2026-11-13 21:13 UTC

This package is auto-updated.

Last update: 2026-11-30 02:14:58 UTC


README

Latest Stable Version Supported PHP Versions License Tests Codecov

Let you register callbacks that will be called when objects are destroyed.

Installation

composer req texthtml/object-reaper

Requires PHP 8.2+.

Usage

$a = (object) [];

// Attach a callback to any object
// It will be called when the object is destroyed

Reaper::watch($a, function () { echo "Good Bye"; });

// "Good bye" will be printed when $a is destroyed

Detaching

Reaper::watch() returns a reaper instance. Call forget() on it to detach the callback: it will no longer be called when the object is destroyed.

$a = (object) [];

$reaper = Reaper::watch($a, function () { echo "Good Bye"; });
$reaper->forget();

unset($a);
// prints nothing

Multiple callbacks

You can attach several callbacks to the same object. They are called in the order they were registered.

$a = (object) [];

Reaper::watch($a, function () { echo "Good"; });
Reaper::watch($a, function () { echo " Bye"; });

unset($a);
// prints "Good Bye"