sminnee/callbacklist

PHP类,用于管理回调列表

0.1.1 2020-12-06 22:00 UTC

This package is auto-updated.

Last update: 2024-08-29 05:53:50 UTC


README

Build Status Scrutinizer Code Quality codecov.io

Latest Stable Version License Monthly Downloads

GitHub Code Size GitHub Last Commit GitHub Activity GitHub Issues

此微包提供了一个简单的类来管理回调列表。

使用方法

> composer require sminnee/callbacklist
use Sminnee\CallbackList\CallbackList;

$list = new CallbackList;
$list->add(function() { "this will get called"; });
$list->add(function() { "so will this"; });
$list->call();

// Or you can use it as a callable if you prefer
$list();

可以传递参数

$list->add(function($greeting) { "$greeting, world!"; });
$list("Hello");

返回值将作为一个数组收集

use Sminnee\CallbackList\CallbackList;

$list = new CallbackList;
$list->add(function() { return "this will get returned"; });
$list->add(function() { return "so will this"; });

// ["this will get returned", "so will this"]
var_dump($list());

可以操作现有的回调

// Clear the list
$list->clear();

// Or add a callback with a name
$list->add(function($greeting) { "$greeting, world!"; }, 'greeter');

// And then remove by that name
$list->remove('greeter');

并且可以检查列表的内容

// Return a single named callback
$list->get('greeter');

// Return everything as an array
$list->getAll();