PHPackages                             charescape/serialize-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. charescape/serialize-closure

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

charescape/serialize-closure
============================

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

3.8.0(3y ago)89.5k↓21.1%3[3 PRs](https://github.com/charescape/serialize-closure/pulls)5MITPHPPHP ^5.4 || ^7.0 || ^8.0

Since Mar 18Pushed 1y ago1 watchersCompare

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

READMEChangelog (1)Dependencies (2)Versions (8)Used By (5)

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

[](#opis-closure)

[![Tests](https://github.com/charescape/serialize-closure/workflows/Tests/badge.svg)](https://github.com/charescape/serialize-closure/actions)[![Latest Stable Version](https://camo.githubusercontent.com/3adc7dfb32a40c9f276d694171952a3aad700a4289ef4f7d7e28a9f8215424a5/68747470733a2f2f706f7365722e707567782e6f72672f636861726573636170652f73657269616c697a652d636c6f737572652f762f737461626c652e706e67)](https://packagist.org/packages/charescape/serialize-closure)[![Latest Unstable Version](https://camo.githubusercontent.com/12938405100f9a368342dc4d6e347e42613fdce4508f48e5699e157e737c5bf1/68747470733a2f2f706f7365722e707567782e6f72672f636861726573636170652f73657269616c697a652d636c6f737572652f762f756e737461626c652e706e67)](https://packagist.org/packages/charescape/serialize-closure)[![License](https://camo.githubusercontent.com/f6a9b8543e155f307d0bd4f18ba1dc34698710f96f2c3f06082bc2f1691a3ce1/68747470733a2f2f706f7365722e707567782e6f72672f6f7069732f636c6f737572652f6c6963656e73652e706e67)](https://packagist.org/packages/charescape/serialize-closure)

Differences compared to the original version
============================================

[](#differences-compared-to-the-original-version)

- Installs as a replacement of [opis/closure](https://github.com/opis/closure) package, uses the same namespaces and classes

```
 composer require charescape/serialize-closure
```

*If you require `opis/closure` directly in your root composer.json, you have to remove it first*

```
composer remove opis/closure
```

- Added support for PHP 8.0, 8.1, 8.2
- Fixed deprecations:
    - [PHP 8.1: Serializable interface deprecated](https://php.watch/versions/8.1/serializable-deprecated)
    - [PHP 8.2: Dynamic Properties are deprecated](https://php.watch/versions/8.2/dynamic-properties-deprecated)

Backward Incompatible Changes
-----------------------------

[](#backward-incompatible-changes)

##### 1. Serialization format and `SerializableClosure` class API

[](#1-serialization-format-and-serializableclosure-class-api)

> **TLDR:** You are affected ONLY if you either:
>
> - Used `JsonSerializableClosure`
> - Signed closures with `SerializableClosure::setSecretKey()`
> - Extended `SerializableClosure` or `JsonSerializableClosure` classes

The [PHP 8.1: Serializable interface deprecation](https://php.watch/versions/8.1/serializable-deprecated)forces us to implement the `__serialize(): array` and `__unserialize(array $data): void` methods.

Since PHP 7.4, when a class gets these magic methods implemented, the `Serializable::serialize(): string`and `Serialize::unserialize(): void` are not called anymore. Check out the following example, to understand how the output looks [with](https://3v4l.org/JmIRQ) and [without](https://3v4l.org/Tp4UF)the `__serialize` and `__unserialize` methods implemented.

It means we can only build the data array we want PHP to serialize to a string, but we can't build the string ourselves to include a signature there. This is why we had to change the serialization format for the `JsonSerializableClosure` class and the `SerializableClosure` when signature verification is enabled.

Action points, if you are affected:

1. If you have serialized closures saved in your database, caches or files – you will need to re-serialize them.
2. If you have extended `SerializableClosure` or `JsonSerializableClosure` classes, you will need to review your code and make the necessary changes.

---

---

Serializable closures
---------------------

[](#serializable-closures)

**Opis Closure** is a library that aims to overcome PHP's limitations regarding closure serialization by providing a wrapper that will make all closures serializable.

**The library's key features:**

- Serialize any closure
- Serialize arbitrary objects
- Doesn't use `eval` for closure serialization or unserialization
- Works with any PHP version that has support for closures
- Supports PHP 7 syntax
- Handles all variables referenced/imported in `use()` and automatically wraps all referenced/imported closures for proper serialization
- Handles recursive closures
- Handles magic constants like `__FILE__`, `__DIR__`, `__LINE__`, `__NAMESPACE__`, `__CLASS__`, `__TRAIT__`, `__METHOD__` and `__FUNCTION__`.
- Automatically resolves all class names, function names and constant names used inside the closure
- Track closure's residing source by using the `#trackme` directive
- Simple and very fast parser
- Any error or exception, that might occur when executing an unserialized closure, can be caught and treated properly
- You can serialize/unserialize any closure unlimited times, even those previously unserialized (this is possible because `eval()` is not used for unserialization)
- Handles static closures
- Supports cryptographically signed closures
- Provides a reflector that can give you information about the serialized closure
- Provides an analyzer for *SuperClosure* library
- Automatically detects when the scope and/or the bound object of a closure needs to be serialized in order for the closure to work after deserialization

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

[](#documentation)

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

License
-------

[](#license)

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

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

[](#requirements)

- PHP ^5.4 || ^7.0 || ^8.0

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

[](#installation)

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

```
composer require charescape/serialize-closure
```

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

```
{
    "require": {
        "charescape/serialize-closure": "^3.8"
    }
}
```

### Migrating from 2.x

[](#migrating-from-2x)

If your project needs to support PHP 5.3 you can continue using the `2.x` version of **Opis Closure**. Otherwise, assuming you are not using one of the removed/refactored classes or features(see [CHANGELOG](https://github.com/charescape/serialize-closure/blob/master/CHANGELOG.md "Changelog")), migrating to version `3.x` is simply a matter of updating your `composer.json` file.

### Semantic versioning

[](#semantic-versioning)

**Opis Closure** follows [semantic versioning](http://semver.org/ "Semantic versioning") specifications.

### Arbitrary object serialization

[](#arbitrary-object-serialization)

We've added this feature in order to be able to support the serialization of a closure's bound object. The implementation is far from being perfect, and it's really hard to make it work flawless. We will try to improve this, but we can't guarantee anything. So our advice regarding the `Opis\Closure\serialize|unserialize` functions is to use them with caution.

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community26

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.8% 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 ~14 days

Total

2

Last Release

1132d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/53265646?v=4)[charescape](/maintainers/charescape)[@charescape](https://github.com/charescape)

---

Top Contributors

[![msarca](https://avatars.githubusercontent.com/u/5808737?v=4)](https://github.com/msarca "msarca (351 commits)")[![charescape](https://avatars.githubusercontent.com/u/53265646?v=4)](https://github.com/charescape "charescape (24 commits)")[![sorinsarca](https://avatars.githubusercontent.com/u/3427195?v=4)](https://github.com/sorinsarca "sorinsarca (21 commits)")[![SilverFire](https://avatars.githubusercontent.com/u/4499203?v=4)](https://github.com/SilverFire "SilverFire (3 commits)")[![GrahamCampbell](https://avatars.githubusercontent.com/u/2829600?v=4)](https://github.com/GrahamCampbell "GrahamCampbell (3 commits)")[![noldor](https://avatars.githubusercontent.com/u/7965547?v=4)](https://github.com/noldor "noldor (3 commits)")[![davidbyoung](https://avatars.githubusercontent.com/u/6652430?v=4)](https://github.com/davidbyoung "davidbyoung (2 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)")[![gadezist](https://avatars.githubusercontent.com/u/39760138?v=4)](https://github.com/gadezist "gadezist (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)")[![kelunik](https://avatars.githubusercontent.com/u/2743004?v=4)](https://github.com/kelunik "kelunik (1 commits)")

---

Tags

serializeserializationserializableclosurefunctionanonymous functions

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[opis/closure

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

2.6k230.0M284](/packages/opis-closure)[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)
