PHPackages                             frodeborli/serializor - 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. frodeborli/serializor

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

frodeborli/serializor
=====================

PHP Serializor will serialize almost anything in PHP, including closures and anonymous classes.

v2.1.0(4mo ago)19225↓75%2MITPHPPHP ^8.1CI passing

Since Sep 5Pushed 4mo ago2 watchersCompare

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

READMEChangelogDependencies (4)Versions (13)Used By (0)

Serializor
==========

[](#serializor)

[![Tests](https://github.com/frodeborli/serializor/actions/workflows/tests.yml/badge.svg)](https://github.com/frodeborli/serializor/actions/workflows/tests.yml)[![Packagist](https://camo.githubusercontent.com/3a86b4d38abfda55178b262a99ea54802c451d1e2f22c23656cc6ef002a92e43/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f66726f6465626f726c692f73657269616c697a6f723f6c6162656c3d5061636b6167697374)](https://packagist.org/packages/frodeborli/serializor)[![PHP Version](https://camo.githubusercontent.com/59a42db101664631fa3c2146932c9108e743da6ba038fb7cd3ba38d6b6a5515d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f66726f6465626f726c692f73657269616c697a6f72)](https://packagist.org/packages/frodeborli/serializor)[![Downloads](https://camo.githubusercontent.com/839917e9f067071623ddab803a8ce3b062d6ae9ee8206cd79f20074333815b1e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f66726f6465626f726c692f73657269616c697a6f723f6c6162656c3d446f776e6c6f616473)](https://packagist.org/packages/frodeborli/serializor)[![License](https://camo.githubusercontent.com/d042766d0dd188874ebf83c878cc0e08c65965cabdead9801d83cdfd4cfa4d3d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f66726f6465626f726c692f73657269616c697a6f723f636f6c6f723d7465616c266c6162656c3d4c6963656e7365)](https://packagist.org/packages/frodeborli/serializor)

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

[](#serialize-closures-and-anonymous-classes)

**Serializor** is a PHP library that allows you to serialize closures, anonymous classes, and arbitrary data - without wrapper classes or code modifications.

Key features:

- serialize [closures](https://www.php.net/manual/en/functions.anonymous.php) without wrapper classes
- serialize [anonymous classes](https://www.php.net/manual/en/language.oop5.anonymous.php)
- works with [readonly properties](https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.readonly-properties) - no `__serialize()` required
- works with typed `Closure` properties (`public readonly Closure $handler`)
- handles circular and recursive references
- supports WeakReference and WeakMap with correct weak semantics
- supports SPL classes (ArrayObject, SplObjectStorage, SplDoublyLinkedList, etc.)
- supports DateTime classes
- extensible via custom transformers
- optional [HMAC signing](#security) for secure cross-machine serialization
- does not rely on PHP extensions (no FFI or similar dependencies)
- supports PHP 8.2 - 8.5

### Example: Closure serialization

[](#example-closure-serialization)

```
use Serializor\Serializor;

$greet = fn($name) => "Hello, $name!";

$serialized = Serializor::serialize($greet);
$restored = Serializor::unserialize($serialized);

echo $restored('World'); // Hello, World!
```

### Example: Anonymous class serialization

[](#example-anonymous-class-serialization)

```
use Serializor\Serializor;

$obj = new class("Hello from anonymous class!") {
    public function __construct(private string $message) {}

    public function greet(): string {
        return $this->message;
    }
};

$serialized = Serializor::serialize($obj);
$restored = Serializor::unserialize($serialized);

echo $restored->greet(); // Hello from anonymous class!
```

### Example: Readonly properties (no class modifications)

[](#example-readonly-properties-no-class-modifications)

```
use Serializor\Serializor;

class MyService {
    public readonly Closure $handler;

    public function __construct() {
        $this->handler = fn($x) => $x * 2;
    }
}

$service = new MyService();
$serialized = Serializor::serialize($service);  // Just works
$restored = Serializor::unserialize($serialized);

echo ($restored->handler)(21); // 42
```

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

[](#installation)

**Serializor** is available on [Packagist](https://packagist.org/packages/frodeborli/serializor "Packagist") and can be installed via [Composer](https://getcomposer.org "Composer"):

```
composer require frodeborli/serializor
```

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

[](#requirements)

- PHP &gt;= 8.2

Security
--------

[](#security)

By default, Serializor does not sign serialized data. For production use, especially in distributed systems or job queues, you should set a shared secret:

```
use Serializor\Serializor;

Serializor::setDefaultSecret('your-shared-secret');
```

When a secret is set, all serialized data is HMAC-signed to prevent tampering.

Custom Serializers
------------------

[](#custom-serializers)

For types that need special handling (like database connections that must be reconnected), you can register custom serialization logic:

```
use Serializor\Stasis;

Stasis::registerFactory(PDO::class, function (PDO $pdo): MyPDOStasis {
    return MyPDOStasis::fromPDO($pdo);
});
```

See [tests/Transformers/CustomTransformerTest.php](tests/Transformers/CustomTransformerTest.php) for a complete example.

Comparison with Other Libraries
-------------------------------

[](#comparison-with-other-libraries)

FeatureSerializoropis/closure 4.xlaravel/serializable-closureClosure serializationYesYesYesAnonymous class serializationYesYesNoNo wrapper classes requiredYesYesNoReadonly `Closure` propertiesYesRequires `__serialize()`NoWeakReference / WeakMapYesYesNoSplObjectStorageYesYesNoHMAC signingYesYesYesTest coverage277 tests~70 tests~130 tests**Serializor's advantages**:

- Works with typed readonly properties and third-party objects without any class modifications
- Most comprehensive test suite covering edge cases from opis/closure GitHub issues
- Supports PHP 8.2-8.5 features including property hooks and pipe operator

Migrating from Laravel or Opis
------------------------------

[](#migrating-from-laravel-or-opis)

Serializor can unserialize data that was serialized by `laravel/serializable-closure` or `opis/closure`, providing a seamless upgrade path. The original library must remain installed for deserialization to work:

```
use Serializor\Serializor;

// Data serialized with Laravel or Opis can be unserialized with Serializor
// (requires the original library to be installed)
$closure = Serializor::unserialize($legacySerializedData);

// Re-serialize with Serializor - no longer requires the old library
$newSerializedData = Serializor::serialize($closure);
```

This allows gradual migration: keep the old library installed while transitioning, then remove it once all stored data has been re-serialized with Serializor.

Known Limitations
-----------------

[](#known-limitations)

- Anonymous classes extending internal PHP classes (stdClass, ArrayObject) cannot be serialized
- Multiple closures on the same line with identical signatures cannot be distinguished (PHP limitation)

History
-------

[](#history)

Serializor was first released on **September 5, 2024**, introducing a novel architecture for PHP closure serialization: direct serialization without wrapper classes, stream wrapper-based reconstruction, WeakMap + ReflectionReference cycle detection, and an extensible transformer system.

Four months later, Opis/Closure v4.0.0 (December 2024) was released as a "complete rewrite" featuring remarkably similar architectural choices. For a detailed technical comparison, see [DESIGN.md](DESIGN.md).

Performance
-----------

[](#performance)

[![Serialization](docs/serialization-benchmark.svg)](docs/serialization-benchmark.svg)

[![Unserialization](docs/unserialization-benchmark.svg)](docs/unserialization-benchmark.svg)

License
-------

[](#license)

**Serializor** is licensed under the [MIT License](https://opensource.org/licenses/MIT "MIT License").

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance74

Regular maintenance activity

Popularity24

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 70.5% 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 ~44 days

Recently: every ~0 days

Total

12

Last Release

140d ago

Major Versions

v1.1.1 → v2.0.0-RC12025-12-29

PHP version history (3 changes)v1.0.0PHP ^7.3|^8.0

v1.1.1PHP ^8.2

v2.1.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/8822e0a14ce04a8f711d4ea933ea90aa005d9ee14827f05feac572c5601fb840?d=identicon)[frodeborli](/maintainers/frodeborli)

---

Top Contributors

[![frodeborli](https://avatars.githubusercontent.com/u/2299344?v=4)](https://github.com/frodeborli "frodeborli (43 commits)")[![oruborus](https://avatars.githubusercontent.com/u/17663?v=4)](https://github.com/oruborus "oruborus (18 commits)")

---

Tags

serializeserializationSerializableClosureopis closure

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/frodeborli-serializor/health.svg)

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

###  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)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[flix-tech/avro-serde-php

A library to serialize and deserialize Avro records making use of the confluent schema registry

674.0M17](/packages/flix-tech-avro-serde-php)

PHPackages © 2026

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