Class for asserting that a callback happens when a dummy object leaves scope

v3.0.0 2018-10-30 23:52 UTC

This package is auto-updated.

Last update: 2021-09-24 06:56:35 UTC


Latest Stable Version License


The ScopedCallback class allows for running a function after the instance goes out of scope. It can be useful for making sure teardown or cleanup functions run even if exceptions are thrown. It also makes for a cleaner API for developers, by not requiring the callback to be called manually each time.

Additional documentation about the library can be found on


use Wikimedia\ScopedCallback;
$sc = new ScopedCallback( [ $this, 'teardown' ] );
// Even if this throws an exception, the callback will run
// or it'll run at the end of the function
// If you want to manually call the callback
ScopedCallback::consume( $sc );
// or
unset( $sc );
// If you want to prevent it from being called
ScopedCallback::cancel( $sc );

Running tests

composer install --prefer-dist
composer test