Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Callback Laravel Package

zenstruck/callback

Tiny PHP utility for building and composing callbacks/closures with a fluent API. Helps wrap callables, bind arguments, decorate or chain behavior, and safely invoke functions and methods—handy for middleware-style pipelines, event hooks, and reusable functional helpers.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install the package via Composer: composer require zenstruck/callback
  2. Create your first Callback:
    use Zenstruck\Callback;
    
    $callback = Callback::from(fn() => 'Hello world');
    echo $callback(); // Invokes the wrapped callable
    
  3. Normalize any callable type:
    $callback = Callback::from('strlen'); // Built-in function
    $callback = Callback::from([MyService::class, 'handle']); // Static method
    $callback = Callback::from($myObject); // Invokable object
    
  4. Start with the Callback class and its static from() factory — that’s your main entry point. Check Callback::__toString() for a human-readable description (great for logs or debugging).

Implementation Patterns

  • Centralize callback logic: Instead of passing raw callables (e.g., closures in event listeners or command handlers), wrap them in Callback for consistent type-hinting (Callback $callback) and safer dispatching.
  • Pass callbacks in config or queues: Because Callback implements __toString() with a normalized representation (e.g., "App\Commands\Handler::handle"), it’s easier to serialize/deserialize than closures (which are unserializable in many contexts).
  • Debugging middleware or listeners: In Laravel pipelines or event listeners, replace raw callables with Callback objects to get meaningful output in logs or developer tools:
    logger((string) $callback); // "MyListener::onUserLogin"
    
  • Factory pattern for dynamic callbacks:
    Callback::from(Closure::fromCallable([MyService::class, 'process']));
    
  • Laravel jobs with deferred callbacks: Store Callback instances as JSON-safe strings in job payloads — ideal for deferred or scheduled work where closures won’t serialize cleanly.

Gotchas and Tips

  • Closures aren’t serializable to string by defaultCallback attempts to represent them as "Closure @line X in file Y", but don’t rely on round-tripping them across processes (e.g., queues). Prefer named callables (methods, static calls, invokables).
  • Callback::from() is smart: It auto-detects Class@method strings, invokables, and arrays — but be explicit when passing closures (use Closure::fromCallable() or just pass them directly; from() handles them, but representation won’t be meaningful).
  • Invoke safely: Callback forwards __invoke() to the wrapped callable — but any exceptions (e.g., missing class/method) bubble up normally. Wrap invocations in try/catch if used in user-facing contexts.
  • Debug tip: Callback::debug($callback) gives a nicely formatted string (using __toString() internally) — useful for dd() or logging.
  • Extensibility: The package is tiny (~100 LOC) — the core logic lives in Callback and CallbackFactory. If you need custom representation (e.g., anonymized method names), extend Callback and override __toString().
  • Last release was in 2022 — verify compatibility with PHP 8.1+ and newer Laravel versions before adoption in critical systems.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation