wikimedia / scoped-callback
Make a callback run when a dummy object leaves the scope.
Package info
github.com/wikimedia/mediawiki-libs-ScopedCallback
pkg:composer/wikimedia/scoped-callback
v6.0.0
2026-10-27 19:50 UTC
Requires
- php: >=8.3
- symfony/polyfill-php85: ^1.33.0
Requires (Dev)
- mediawiki/mediawiki-codesniffer: 52.0.0
- mediawiki/mediawiki-phan-config: 0.20.0
- mediawiki/minus-x: 2.0.1
- ockcyp/covers-validator: 1.7.0
- php-parallel-lint/php-console-highlighter: 1.0.0
- php-parallel-lint/php-parallel-lint: 1.4.0
- phpunit/phpunit: 10.5.63
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
ScopedCallback
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 MediaWiki.org.
Usage
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 $this->fooBar(); // 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