PHPackages                             opis/closure - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. opis/closure

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

opis/closure
============

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

4.5.0(2mo ago)2.6k230.0M—2%9420MITPHPPHP ^8.0CI failing

Since Feb 12Pushed 2mo ago23 watchersCompare

[ Source](https://github.com/opis/closure)[ Packagist](https://packagist.org/packages/opis/closure)[ Docs](https://opis.io/closure)[ RSS](/packages/opis-closure/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (73)Used By (20)

Opis Closure
============

[](#opis-closure)

[![Tests](https://github.com/opis/closure/workflows/Tests/badge.svg)](https://github.com/opis/closure/actions)[![Packagist Version](https://camo.githubusercontent.com/50b886a3b8f5674439290130791d36ffc9be770b5e3a5e490d97ffc9d2195de1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f7069732f636c6f737572653f6c6162656c3d56657273696f6e)](https://packagist.org/packages/opis/closure)[![Packagist Downloads](https://camo.githubusercontent.com/5894077694dd1b78f0109444d2d66e256deb2d6f397ddcf9895b1beceb3d45f4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f7069732f636c6f737572653f6c6162656c3d446f776e6c6f616473)](https://packagist.org/packages/opis/closure)[![Packagist License](https://camo.githubusercontent.com/d823968761582afa6f258e14b250c21a448bc814c9890c30f6fc9ac501b54f1e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6f7069732f636c6f737572653f636f6c6f723d7465616c266c6162656c3d4c6963656e7365)](https://packagist.org/packages/opis/closure)

Serialize closures and anonymous classes
----------------------------------------

[](#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)](https://www.php.net/manual/en/functions.anonymous.php)
- serialize [anonymous classes](https://www.php.net/manual/en/language.oop5.anonymous.php)
- does not rely on PHP extensions (no FFI or similar dependencies)
- supports PHP 8.0-8.5 syntax
- handles circular references
- works with [attributes](https://www.php.net/manual/en/language.attributes.overview.php)
- works with [readonly properties](https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.readonly-properties)
- works with [property hooks](https://www.php.net/manual/en/language.oop5.property-hooks.php)
- extensible via [custom serializers and deserializers](https://opis.io/closure/4.x/objects.html)
- supports [cryptographically signed data](https://opis.io/closure/4.x/security.html)
- supports PHP's built-in [SPL and Date classes](https://opis.io/closure/4.x/objects.html#default-object-serializers), and the popular [`nesbot/carbon`](https://github.com/CarbonPHP/carbon) package
- reconstructed code is close to the original and [debugger friendly](https://opis.io/closure/4.x/debug.html)
- and [many more](https://opis.io/closure/4.x/ "Opis Closure Documentation")

### Example of closure serialization

[](#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

[](#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
------------------

[](#migrating-from-3x)

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](https://opis.io/closure/4.x/migrate.html "Opis Closure Migration guide").

Documentation
-------------

[](#documentation)

The full documentation for this library can be found [here](https://opis.io/closure/4.x/ "Opis Closure Documentation").

License
-------

[](#license)

**Opis Closure** is licensed under the [MIT License (MIT)](http://opensource.org/licenses/MIT "MIT License").

Requirements
------------

[](#requirements)

- PHP &gt;= 8.0

Installation
------------

[](#installation)

**Opis Closure** is available on [Packagist](https://packagist.org/packages/opis/closure "Packagist"), and it can be installed from a command line interface by using [Composer](https://getcomposer.org "Composer"):

```
composer require opis/closure
```

Or you could directly reference it into your `composer.json` file as a dependency

```
{
    "require": {
        "opis/closure": "^4.5"
    }
}
```

###  Health Score

78

—

ExcellentBetter than 100% of packages

Maintenance86

Actively maintained with recent releases

Popularity80

Widely adopted with strong download metrics

Community50

Growing community involvement

Maturity83

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 86% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~63 days

Recently: every ~104 days

Total

71

Last Release

74d ago

Major Versions

1.3.x-dev → 2.0.02015-07-30

2.4.0 → 3.0.02017-04-07

2.4.x-dev → 3.0.12017-04-13

3.6.3 → 4.0.02024-12-28

3.7.0 → 4.4.02025-11-20

PHP version history (5 changes)1.0.0PHP &gt;=5.3.0

3.0.0PHP &gt;=5.4.0

3.1.0PHP ^5.4 || ^7.0

3.6.0PHP ^5.4 || ^7.0 || ^8.0

4.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/47afaf3c711dd321c8d09bae2afed928971ec9e9a7593c925749696f04d2fddd?d=identicon)[msarca](/maintainers/msarca)

![](https://www.gravatar.com/avatar/7c720b40eb280d9fd33cc35d74c34b3763c2777bd2502d094625faa367f0dca4?d=identicon)[sorinsarca](/maintainers/sorinsarca)

---

Top Contributors

[![msarca](https://avatars.githubusercontent.com/u/5808737?v=4)](https://github.com/msarca "msarca (351 commits)")[![sorinsarca](https://avatars.githubusercontent.com/u/3427195?v=4)](https://github.com/sorinsarca "sorinsarca (38 commits)")[![noldor](https://avatars.githubusercontent.com/u/7965547?v=4)](https://github.com/noldor "noldor (3 commits)")[![GrahamCampbell](https://avatars.githubusercontent.com/u/2829600?v=4)](https://github.com/GrahamCampbell "GrahamCampbell (3 commits)")[![davidbyoung](https://avatars.githubusercontent.com/u/6652430?v=4)](https://github.com/davidbyoung "davidbyoung (2 commits)")[![kelunik](https://avatars.githubusercontent.com/u/2743004?v=4)](https://github.com/kelunik "kelunik (1 commits)")[![philipobenito](https://avatars.githubusercontent.com/u/2077289?v=4)](https://github.com/philipobenito "philipobenito (1 commits)")[![randstring](https://avatars.githubusercontent.com/u/16113843?v=4)](https://github.com/randstring "randstring (1 commits)")[![rasj](https://avatars.githubusercontent.com/u/2250535?v=4)](https://github.com/rasj "rasj (1 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (1 commits)")[![abdrzakoxa](https://avatars.githubusercontent.com/u/44305005?v=4)](https://github.com/abdrzakoxa "abdrzakoxa (1 commits)")[![taylorotwell](https://avatars.githubusercontent.com/u/463230?v=4)](https://github.com/taylorotwell "taylorotwell (1 commits)")[![billdanbury](https://avatars.githubusercontent.com/u/358948?v=4)](https://github.com/billdanbury "billdanbury (1 commits)")[![deleugpn](https://avatars.githubusercontent.com/u/9533181?v=4)](https://github.com/deleugpn "deleugpn (1 commits)")[![jan-di](https://avatars.githubusercontent.com/u/23323185?v=4)](https://github.com/jan-di "jan-di (1 commits)")[![KarelWintersky](https://avatars.githubusercontent.com/u/2164874?v=4)](https://github.com/KarelWintersky "KarelWintersky (1 commits)")

---

Tags

anonymous-class-serializationclosure-serializationdata-serializationphpserializable-closuresserializationserializeserializationserializableclosurefunctionanonymous functionsanonymous classes

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/opis-closure/health.svg)

```
[![Health](https://phpackages.com/badges/opis-closure/health.svg)](https://phpackages.com/packages/opis-closure)
```

###  Alternatives

[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k135.8M851](/packages/jms-serializer)[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11120.2M21](/packages/anourvalar-eloquent-serialize)[jeremeamia/functionparser

Function parser for PHP functions, methods, and closures

48169.7k6](/packages/jeremeamia-functionparser)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
