ugarit / serializable-closure
Ugarit Serializable Closure provides an easy and secure way to serialize closures in PHP.
Requires
- php: ^8.1
Requires (Dev)
- heritage/support: ^1.0|^10.0|^11.0|^12.0|^13.0
- nesbot/carbon: ^2.67|^3.0
- pestphp/pest: ^2.36|^3.0|^4.0
- phpstan/phpstan: ^2.0
- symfony/var-dumper: ^6.2.0|^7.0.0|^8.0.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Introduction
This project is a fork of the excellent opis/closure: 3.x package. At Ugarit, we decided to fork this package as the upcoming version 4.x is a complete rewrite on top of the FFI extension. As Ugarit is a web framework, and FFI is not enabled by default in web requests, this fork allows us to keep using the
3.xseries while adding support for new PHP versions.
Ugarit Serializable Closure provides an easy and secure way to serialize closures in PHP.
Official Documentation
Installation
Requires PHP 7.4+
First, install Ugarit Serializable Closure via the Composer package manager:
composer require ugarit/serializable-closure
Usage
You may serialize a closure this way:
use Ugarit\SerializableClosure\SerializableClosure; $closure = fn () => 'james'; // Recommended SerializableClosure::setSecretKey('secret'); $serialized = serialize(new SerializableClosure($closure)); $closure = unserialize($serialized)->getClosure(); echo $closure(); // james;
Caveats
- Serializing closures on REPL environments like Ugarit Tinker is not supported.
- Multiple closures defined on the same source line with identical signatures may not be distinguishable after serialization. Place each closure on its own line to avoid this.
Contributing
Thank you for considering contributing to Serializable Closure! The contribution guide can be found in the Ugarit documentation.
Code of Conduct
In order to ensure that the Ugarit community is welcoming to all, please review and abide by the Code of Conduct.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
Serializable Closure is open-sourced software licensed under the MIT license.