Quick start
Learn how to serialize data
Serialize closures, serialize anything
This library provides an enhanced version of PHP’s
serialize
and
unserialize
functions.
It supports serializing arbitrary objects, arrays, closures, and enums, handles circular references, and provides an
API for custom object serialization.
use function Opis\Closure\{serialize, unserialize};
$closure = fn() => "it works";
// serialize closure (or arbitrary data)
$serialized = serialize($closure);
// unserialize closure
$unserialized = unserialize($serialized);
echo $unserialized(); //> it works
Data signing
We often send serialized data to other services we control, and at deserialization, we want to ensure that the data has not been tampered with and is safe to use. Opis Closure provides a straightforward way of signing data and verifying its integrity.
use Opis\Closure\Security\{
DefaultSecurityProvider,
SecurityProviderInterface,
};
use function Opis\Closure\init;
// Use the DefaultSecurityProvider (shortcut)
init("my-secret-key");
// Use the DefaultSecurityProvider
init(new DefaultSecurityProvider("my-secret-key"));
// Use a custom security provider
init(new class() implements SecurityProviderInterface {
public function sign(string $data): string {
// ...
}
public function verify(string $hash, string $data): bool {
// ...
}
});
Read more about data signing.