opis/closure
Serializable closures for PHP: safely capture and transport anonymous functions across processes or requests. Useful for caching, queues, sessions, and distributed jobs, with support for binding, scope, and restoring closures without manual refactoring.
Opis Closure is a PHP library that allows you to serialize closures, anonymous classes, and arbitrary data.
Key features:
nesbot/carbon packageuse function Opis\Closure\{serialize, unserialize};
$serialized = serialize(fn() => "hello from closure!");
$greet = unserialize($serialized);
echo $greet(); // hello from closure!
use function Opis\Closure\{serialize, unserialize};
$serialized = serialize(new class("hello from anonymous class!") {
public function __construct(private string $message) {}
public function greet(): string {
return $this->message;
}
});
$object = unserialize($serialized);
echo $object->greet(); // hello from anonymous class!
Version 4.x is a full rewrite of the library, but data deserialization from 3.x is possible. Read the docs on how to migrate from 3.x.
The full documentation for this library can be found here.
Opis Closure is licensed under the MIT License (MIT).
Opis Closure is available on Packagist, and it can be installed from a command line interface by using Composer:
composer require opis/closure
Or you could directly reference it into your composer.json file as a dependency
{
"require": {
"opis/closure": "^4.5"
}
}
How can I help you explore Laravel packages today?