opis / closure
A library that can be used to serialize closures (anonymous functions) and arbitrary data.
Installs: 224 707 317
Dependents: 313
Suggesters: 13
Security: 0
Stars: 2 548
Watchers: 23
Forks: 93
Open Issues: 0
pkg:composer/opis/closure
Requires
- php: ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- dev-master / 4.x-dev
- 4.4.0
- 4.3.1
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.1
- 4.0.0
- 3.x-dev
- 3.7.0
- 3.6.3
- 3.6.2
- 3.6.1
- 3.6.0
- 3.5.7
- 3.5.6
- 3.5.5
- 3.5.4
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5.0
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.1
- 3.3.0
- 3.2.0
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.4.x-dev
- 2.4.1
- 2.4.0
- 2.3.x-dev
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.x-dev
- 2.2.1
- 2.2.0
- 2.1.x-dev
- 2.1.0
- 2.0.1
- 2.0.0
- 1.3.x-dev
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.0
- dev-php8.5-support
- dev-ffi
This package is auto-updated.
Last update: 2025-11-20 00:49:37 UTC
README
Serialize closures and anonymous classes
Opis Closure is a PHP library that allows you to serialize closures, anonymous classes, and arbitrary data.
Key features:
- serialize closures (anonymous functions)
- serialize anonymous classes
- does not rely on PHP extensions (no FFI or similar dependencies)
- supports PHP 8.0-8.5 syntax
- handles circular references
- works with attributes
- works with readonly properties
- works with property hooks
- extensible via custom serializers and deserializers
- supports cryptographically signed data
- supports PHP's built-in SPL and Date classes, and the popular
nesbot/carbonpackage - reconstructed code is close to the original and debugger friendly
- and many more
Example of closure serialization
use function Opis\Closure\{serialize, unserialize}; $serialized = serialize(fn() => "hello from closure!"); $greet = unserialize($serialized); echo $greet(); // hello from closure!
Example of anonymous class serialization
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!
Migrating from 3.x
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.
Documentation
The full documentation for this library can be found here.
License
Opis Closure is licensed under the MIT License (MIT).
Requirements
- PHP >= 8.0
Installation
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.4"
}
}