Let you register callbacks that will be called when objects are destroyed.
composer req texthtml/object-reaperRequires PHP 8.2+.
$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 destroyedReaper::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 nothingYou 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"