wikimedia/scoped-callback

在模拟对象离开作用域时执行回调。

v4.0.0 2022-07-08 22:48 UTC

This package is auto-updated.

Last update: 2024-09-12 06:04:05 UTC


README

Latest Stable Version License

ScopedCallback

ScopedCallback 类允许在实例超出作用域后运行函数。它对于确保即使在抛出异常的情况下也能运行拆解或清理函数非常有用。它还通过不需要在每次手动调用回调来为开发者提供一个更清晰的 API。

有关此库的更多文档可以在 MediaWiki.org 上找到。

用法

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 );

运行测试

composer install --prefer-dist
composer test