Note: This is an abandoned experiment of closure serialization using ffi extension.
Opis Closure is a PHP library that allows you to serialize closures without breaking a sweat. All you have to do is to add a single line of code, and you are good to go.
\Opis\Closure\Library::init();If you are using this library in a server environment, and you have preload enabled (which you should), then add the following line of code in your preload file:
\Opis\Closure\Library::preload();Now you can serialize/unserialize closures the same way you would serialize/unserialize any other data structure.
$f = fn() => 'Hello';
$data = serialize($f);
$g = unserialize($data);
echo $g(); //> HelloThis version of Opis Closure is a full rebuild of the library and is not compatible with the previous versions. The library use FFI to make closures serializable and you no longer need to wrap them as it was the case in the past.
- PHP ^7.4 | ^8.0
- FFI