opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

Package info

github.com/opis/closure

Homepage

pkg:composer/opis/closure

Statistics

Installs: 234 264 226

Dependents: 331

Suggesters: 14

Stars: 2 570

Open Issues: 0

4.5.0 2026-03-05 13:32 UTC

Requires

  • php: ^8.0

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT b97e42b95bb72d87507f5e2d137ceb239aea8d6b

  • Marius Sarca <marius.sarca.woop@gmail.com>
  • Sorin Sarca <sarca_sorin.woop@hotmail.com>

serializeserializationfunctionclosureserializableanonymous functionsanonymous classes


README

👁 Tests
👁 Packagist Version
👁 Packagist Downloads
👁 Packagist License

Serialize closures and anonymous classes

Opis Closure is a PHP library that allows you to serialize closures, anonymous classes, and arbitrary data.

Key features:

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.5"
 }
}