PHPackages                             schlaus/flexiblecallback - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. schlaus/flexiblecallback

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

schlaus/flexiblecallback
========================

FlexibleCallback allows you to unregister or modify callbacks after they have been registered.

v1.0.2(11y ago)1161MITPHP

Since Mar 25Pushed 11y ago1 watchersCompare

[ Source](https://github.com/schlaus/FlexibleCallback)[ Packagist](https://packagist.org/packages/schlaus/flexiblecallback)[ RSS](/packages/schlaus-flexiblecallback/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (4)Used By (0)

FlexibleCallback
================

[](#flexiblecallback)

[![Build Status](https://camo.githubusercontent.com/67776f1a50832928d7ea936bf98d260bb90d3d1e87e52ad73a91ce332f3a7d15/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7363686c6175732f466c657869626c6543616c6c6261636b2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/schlaus/FlexibleCallback)[![Coverage Status](https://camo.githubusercontent.com/e48a147a62b6aa5e6cdca5ac02dbb10efcd103be227ab14394cf9564c6846673/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f7363686c6175732f466c657869626c6543616c6c6261636b2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/r/schlaus/FlexibleCallback?branch=master)[![Latest Version](https://camo.githubusercontent.com/f2add6015e66660523bd8b713261ace16779cc1a3ca8025ac7e569da694713eb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f7363686c6175732f466c657869626c6543616c6c6261636b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/schlaus/flexiblecallback)[![Total Downloads](https://camo.githubusercontent.com/394cc4616642ae945288e5a888e34eaeecb473cf62705aa27ec0a10fd43a0f48/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7363686c6175732f466c657869626c6543616c6c6261636b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/schlaus/flexiblecallback)[![Issues open](https://camo.githubusercontent.com/43d07ca937c287827175b22a48fc345baed27ee1166fdfa1327727f0fa5a0987/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f7363686c6175732f466c657869626c6543616c6c6261636b2e7376673f7374796c653d666c61742d737175617265)](https://github.com/schlaus/FlexibleCallback/issues)[![MIT license](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](http://schlaus.mit-license.org)

FlexibleCallback allows for more flexibility in callback functions.

Specifically, it allows you to:

- Unregister callbacks even if it's not otherwise possible
- Change an already registered callback
- Create callback queues
- Simply return values instead of running a function

#### So where would I need something like that?

[](#so-where-would-i-need-something-like-that)

Well, for example `register_shutdown_function()` won't allow you to unregister or change a callback once it's been registered. FlexibleCallback allows you to circumvent that limitation.

Installation
------------

[](#installation)

```
composer require schlaus/flexiblecallback

```

...or just download and include `FlexibleCallback.php`. There are no dependencies, so you're good to go.

Usage
-----

[](#usage)

#### The basics

[](#the-basics)

```
use Schlaus\FlexibleCallback\FlexibleCallback;

$closure = new FlexibleCallback(function() {
    return "Hello, world!";
});

$closure();     // "Hello, world!"

// register_shutdown_function($closure);

$plainValue = new FlexibleCallback("I'm a return value");

$plainValue();  // "I'm a return value"

$queue = new FlexibleCallback(
    array(
        function() {
            return "Callback 1";
        },
        function() {
            return "Callback 2";
        }
    )
);

$queue();       // "Callback 2"

$queue->unregister();

$queue();       // null
```

#### Change a callback afterwards

[](#change-a-callback-afterwards)

```
$callback = new FlexibleCallback("I don't do anything");

$callback->setCallback("I'm much more useful");

$callback();    // "I'm much more useful"

$callback->setCallback(function() {
    return "But I'm the best";
});

$callback();    // "But I'm the best"

// To enforce a callback type there are two handy methods

$callback = new FlexibleCallback;

$callback->setReturnValue("I'm a string")   // Only allows non-callables

$callback->setCallbackFunction(function() { // Only allows callables
    // Awesome things
});

$callback->setCallbackFunction("But I'm not a function!") // InvalidArgumentException
```

#### Callback queues

[](#callback-queues)

```
// Create a queue while you're instancing the class...
$queue = new FlexibleCallback(array(
    function() {},
    function() {},
    //...
));
// NOTE: This is a queue, not a stack. First in is first out.

// ... or change an already existing object into a queue
$queue = new FlexibleCallback;
$queue->pushCallbackFunction(function(){});
$queue->pushCallbackFunction(function(){});

// A queue can only contain callables, not values
$queue = new FlexibleCallback("string");
$queue->pushCallbackFunction(function() {
    return "I'm a function";
});

$queue();       // "I'm a function"
```

#### Callback arguments in queues

[](#callback-arguments-in-queues)

```
// By default, the same arguments are passed to each callback.
// If a callback in queue returns false, queue execution stops.
// It's possible to change this behaviour so, that instead
// return values are given as the first argument to subsequent
// callbacks.

$queue = new FlexibleCallback;
$queue->includePreviousReturnValue();

$queue = setCallbackFunction(array(
    function($arg1, $arg2) {
        // Since this is the first callback, $arg1 is always null
        // and $arg2 is the first argument passed by the caller
        return false;
    },
    function($arg1, $arg2) {
        // $arg1 is now false, and $arg2 is the same as it was
        // in the previous callback.
    }
));

$queue->excludePreviousReturnValue(); // back to default
```

#### Checking what's been loaded

[](#checking-whats-been-loaded)

```
$callback = new FlexibleCallback("Testing!");

$callback->getCallback();   // "Testing!"

$callback->getType();       // "value"

$callback->pushCallbackFunction(function() {
    return "Hello!";
});

$callback->getType();       // "callable"

$function = $callback->getCallback();

$function();                // "Hello!"

$callback->pushCallbackFunction(/*...*/);

$callback->getType();       // "queue"

// $queue will contain all callbacks in queue
$queue = $callback->getCallback();

$callback->setReturnValue(null);

$callback->getType();       // null

$callback->unregister();

$callback->getType();       // null
```

#### Converting the queue to a stack (sort of)

[](#converting-the-queue-to-a-stack-sort-of)

```
$callback->setCallbackFunction(
    array_reverse($callback->getCallback())
);
```

License
-------

[](#license)

[MIT](http://schlaus.mit-license.org)

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

4072d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c3a43b3880ecbadd178a300fa7f0ff173dc56622c506c9f85822864f658df5d6?d=identicon)[schlaus](/maintainers/schlaus)

---

Top Contributors

[![vinicius73](https://avatars.githubusercontent.com/u/1561347?v=4)](https://github.com/vinicius73 "vinicius73 (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/schlaus-flexiblecallback/health.svg)

```
[![Health](https://phpackages.com/badges/schlaus-flexiblecallback/health.svg)](https://phpackages.com/packages/schlaus-flexiblecallback)
```

###  Alternatives

[getsolaris/laravel-make-service

A MVCS pattern create a service command for Laravel 5+

81161.3k](/packages/getsolaris-laravel-make-service)[ikkez/f3-flash

Add simple Flash Messages and Flash Keys to PHP Fat-Free Framework

1926.0k5](/packages/ikkez-f3-flash)[mfd/ai-filemetadata

Automatically generates FAL metadata for files by means of public LLMs

1142.1k](/packages/mfd-ai-filemetadata)[latfur/laravel-event-crud

Laravel Event CRUD With Full Calendar

125.6k](/packages/latfur-laravel-event-crud)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
