Reagordi Serializable Closure provides an easy and secure way to serialize closures in PHP.
- PHP 8.2 or higher.
The package could be installed with composer:
composer require reagordi/serializable-closure
You may serialize a closure this way:
use Reagordi\Component\SerializableClosure\SerializableClosure;
$closure = fn () => 'james';
// Recommended
SerializableClosure::setSecretKey('secret');
$serialized = serialize(new SerializableClosure($closure));
$closure = unserialize($serialized)->getClosure();
echo $closure(); // james;
- Anonymous classes cannot be created within closures.
- Attributes cannot be used within closures.
- Serializing closures on REPL environments like Laravel Tinker is not supported.
- Serializing closures that reference objects with readonly properties is not supported.
The package is tested with PHPUnit. To run tests:
./vendor/bin/phpunit
The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:
./vendor/bin/roave-infection-static-analysis-plugin
The code is statically analyzed with Psalm. To run static analysis:
./vendor/bin/psalm
Use Rector to make codebase follow some specific rules or use either newest or any specific version of PHP:
./vendor/bin/rector
Use ComposerRequireChecker to detect transitive Composer dependencies.
The Reagordi Serializable Closure is free software. It is released under the terms of the BSD License.
Please see LICENSE
for more information.
Maintained by Reagordi Group.