| 1 |
[![Latest Stable Version]](https://packagist.org/packages/wikimedia/scoped-callback) [![License]](https://packagist.org/packages/wikimedia/scoped-callback) |
| 2 |
|
| 3 |
ScopedCallback |
| 4 |
============== |
| 5 |
|
| 6 |
The ScopedCallback class allows for running a function after the |
| 7 |
instance goes out of scope. It can be useful for making sure |
| 8 |
teardown or cleanup functions run even if exceptions are thrown. |
| 9 |
It also makes for a cleaner API for developers, by not requiring |
| 10 |
the callback to be called manually each time. |
| 11 |
|
| 12 |
Additional documentation about the library can be found on |
| 13 |
[MediaWiki.org](https://www.mediawiki.org/wiki/ScopedCallback). |
| 14 |
|
| 15 |
|
| 16 |
Usage |
| 17 |
----- |
| 18 |
|
| 19 |
```php |
| 20 |
use Wikimedia\ScopedCallback; |
| 21 |
$sc = new ScopedCallback( [ $this, 'teardown' ] ); |
| 22 |
// Even if this throws an exception, the callback will run |
| 23 |
// or it'll run at the end of the function |
| 24 |
$this->fooBar(); |
| 25 |
// If you want to manually call the callback |
| 26 |
ScopedCallback::consume( $sc ); |
| 27 |
// or |
| 28 |
unset( $sc ); |
| 29 |
// If you want to prevent it from being called |
| 30 |
ScopedCallback::cancel( $sc ); |
| 31 |
``` |
| 32 |
|
| 33 |
|
| 34 |
Running tests |
| 35 |
------------- |
| 36 |
|
| 37 |
composer install --prefer-dist |
| 38 |
composer test |
| 39 |
|
| 40 |
|
| 41 |
--- |
| 42 |
[Latest Stable Version]: https://poser.pugx.org/wikimedia/scoped-callback/v/stable.svg |
| 43 |
[License]: https://poser.pugx.org/wikimedia/scoped-callback/license.svg |