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

Superclosure Laravel Package

jeremeamia/superclosure

Serialize and unserialize PHP Closures/anonymous functions, including use() context, via fast TokenAnalyzer or more robust AstAnalyzer. Note: project is no longer maintained; consider using opis/closure instead.

View on GitHub
Deep Wiki
Context7

Getting Started

jeremeamia/superclosure lets you serialize PHP closures — something PHP doesn’t natively support. Despite being unmaintained since 2018 (and explicitly discouraged for new projects in favor of opis/closure), it still works well in legacy codebases or constrained environments where it’s already in use.

First steps:

  1. Install: composer require jeremeamia/superclosure
  2. Import the Serializer and serialize/unserialize closures as needed:
use SuperClosure\Serializer;

$serializer = new Serializer();
$closure = function ($name) { echo "Hello, $name!"; };
$serialized = $serializer->serialize($closure);
$unserialized = $serializer->unserialize($serialized);
$unserialized('World'); // outputs: Hello, World!

This is especially useful when storing closures for delayed execution — e.g., queuing jobs (Laravel historically used it for IronMQ jobs), caching router/container definitions, or persisting simple callable tasks.

Implementation Patterns

  • Job Queues: Push closures to queues (e.g., IronMQ, Redis) as lightweight jobs without defining a separate job class.
$queue->push($serializer->serialize(function ($payload) {
    sendEmail($payload['to'], $payload['subject']);
}));
  • Caching DI Containers/Routers: Serialize closures used in container bindings or route definitions to avoid rebuilding them on every request.
$key = 'router_closures';
$routes = $cache->get($key) ?: $serializer->serialize($router->getClosures());
$cache->put($key, $routes, 3600);
  • Benchmarking / Analysis: Use AstAnalyzer (default) or TokenAnalyzer to introspect closure structure, bindings, scope, and code for debugging or tooling.
use SuperClosure\Analyzer\AstAnalyzer;
$analyzer = new AstAnalyzer();
$metadata = $analyzer->analyze($fn); // includes code, context, binding, location, etc.
  • Signing for Security: Use a signing key to protect against tampering when storing closures:
$serializer = new Serializer(null, 'your-super-secret-signing-key');
$signed = $serializer->serialize($fn);
// Later, unserialize safely — invalid signatures throw `ClosureUnserializationException`
$fn = $serializer->unserialize($signed);

Gotchas and Tips

  • eval() is required during unserialization — always verify serialized data sources are trusted. Prefer signing, and sanitize/unpack closures in restricted contexts (e.g., internal queues, local caches).
  • References are not preserved, except for recursive closure references (use (&$self)).
  • Closures defined on the same line will fail silently — avoid defining multiple anonymous functions in one statement.
  • Cannot serialize closures defined in eval() — including unserialized closures (no re-serialization allowed).
  • Choose analyzers wisely: TokenAnalyzer is ~20× faster but lacks support for class type hints in params/body, static closures, magic constants, etc. Use AstAnalyzer for completeness unless performance is critical.
  • PHP version caveats: v2.4.0 added PHP 7.2/8 support via PHP-Parser 4 — ensure you’re on at least v2.4 for modern PHP compatibility.
  • Deprecation warning: Since this package is no longer maintained, plan migration to opis/closure, which has better docs, active maintenance, and fewer eval() risks (supports AST-based code generation).
  • Avoid in new projects: Unless constrained by legacy dependencies (e.g., older Laravel versions pre-5.5+), prefer alternatives like opis/closure or explicit job classes instead of closures for serialization.
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
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
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