laravel/serializable-closure
Securely serialize and unserialize PHP closures with Laravel’s fork of opis/closure 3.x, updated for modern PHP without requiring FFI. Wrap closures in SerializableClosure, set a secret key for signing, serialize safely, then restore with getClosure().
Bus::chain or delayed jobs) to persist logic across processes without rebuilding the closure.Use This Package If:
opis/closure.Look Elsewhere If:
fn() => 42—use native serialize instead).*"This package lets us store and reuse PHP logic dynamically—like saving a ‘rule engine’ or ‘user-specific workflow’ in a database or cache, then executing it later. It’s critical for:
*"This is a drop-in solution for serializing closures in Laravel, solving pain points like:
Bus::chain with closures that reference $user).Event::listen(fn($user) => ...)).
Why this fork?// Serialize
$serialized = serialize(new SerializableClosure(fn($x) => $x * 2));
cache()->put('rule:double', $serialized);
// Deserialize
$closure = unserialize(cache()->get('rule:double'))->getClosure();
$closure(5); // 10
```*
**Ask**: Should we standardize this for all queue jobs/cached logic?"*
How can I help you explore Laravel packages today?