opis / closure
A library that can be used to serialize closures (anonymous functions) and arbitrary data.
Installs: 207 099 854
Dependents: 270
Suggesters: 10
Security: 0
Stars: 2 524
Watchers: 26
Forks: 87
Open Issues: 0
Requires
- php: ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- dev-master / 4.x-dev
- 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.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-reference-bug
- dev-ffi
This package is auto-updated.
Last update: 2025-01-10 21:50:20 UTC
README
Serialize closures, serialize anything
Opis Closure is a PHP library that allows you to serialize closures, anonymous classes, and arbitrary data.
use function Opis\Closure\{serialize, unserialize}; $serialized = serialize(fn() => "hello from closure!"); $greet = unserialize($serialized); echo $greet(); // hello from closure!
Important
Starting with version 4.2, Opis Closure supports serialization of anonymous classes.
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!
A full rewrite was necessary to keep this project compatible with the PHP's new features, such as attributes, enums, read-only properties, named parameters, anonymous classes, and so on. This wasn't an easy task, as the latest attempt to launch a 4.x version involved using the FFI extension in exotic ways, and it failed hard. The main problem was that very often the closures were bound to some object, thus in order to preserve functionality, we had to serialize the object too. Since we had to do arbitrary data serialization, we decided to make this project about arbitrary data serialization, providing support for serializing closures but also adding more effortless ways to serialize custom objects.
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.3" } }