laminas/laminas-serializer
Serialize and deserialize PHP data using multiple adapters (e.g., PHP native, JSON, XML, WDDX) with consistent options, error handling, and extensibility. Useful for caching, sessions, message payloads, and storage across different formats.
laminas-serialzier provides an adapter-based interface for serializing and deserializing PHP types to and from different representations.
For more information what a serializer is read the wikipedia page of Serialization.
Serializing adapters can either be created from the provided
Laminas\Serializer\Serializer::factory method, or by instantiating one of the
Laminas\Serializer\Adapter\* classes.
use Laminas\Serializer\Adapter;
use Laminas\Serializer\Exception;
use Laminas\Serializer\Serializer;
// Via factory:
$serializer = Serializer::factory(Adapter\PhpSerialize::class);
// Alternately:
$serializer = new Adapter\PhpSerialize();
// Now $serializer is an instance of Laminas\Serializer\Adapter\AdapterInterface,
// specifically Laminas\Serializer\Adapter\PhpSerialize
try {
$serialized = $serializer->serialize($data);
// now $serialized is a string
$unserialized = $serializer->unserialize($serialized);
// now $data == $unserialized
} catch (Exception\ExceptionInterface $e) {
echo $e;
}
The method serialize() generates a storable string. To regenerate this
serialized data, call the method unserialize().
Any time an error is encountered serializing or unserializing, the adapter will
throw a Laminas\Serializer\Exception\ExceptionInterface.
Because an application often uses only one serializer internally, it is possible
to define and use a default serializer. That serializer will be used by default
by other components like Laminas\Cache\Storage\Plugin\Serializer.
To define and use the default serializer, use the static serialization methods
of the basic Laminas\Serializer\Serializer:
use Laminas\Serializer\Adapter;
use Laminas\Serializer\Exception;
use Laminas\Serializer\Serializer;
Serializer::setDefaultAdapter(Adapter\PhpSerialize::class);
try {
$serialized = Serializer::serialize($data);
// now $serialized is a string
$unserialized = Serializer::unserialize($serialized);
// now $data == $unserialized
} catch (Exception\ExceptionInterface $e) {
echo $e;
}
To configure a serializer adapter, you can optionally use an instance of
Laminas\Serializer\Adapter\AdapterOptions, an instance of one of the adapter
specific options class, an array, or a Traversable object. The adapter
will convert it into the adapter specific options class instance (if present) or
into the basic Laminas\Serializer\Adapter\AdapterOptions class instance.
Options can be passed as the second argument to the provided
Laminas\Serializer\Serializer::factory and ::setDefaultAdapter methods, via the
adapter's setOptions method, or as constructor arguments when directly
instantiating an adapter.
Each serializer implements the interface Laminas\Serializer\Adapter\AdapterInterface.
This interface defines the following methods:
| Method signature | Description |
|---|---|
serialize(mixed $value) : string |
Generates a storable representation of a value. |
unserialize(string $value) : mixed |
Creates a PHP value from a stored representation. |
The base class Laminas\Serializer\Serializer is used to instantiate the
adapters, to configure the factory, and as a proxy for serialization operations.
It defines the following static methods, where the following references map to classes/interfaces as follows:
AdapterPluginManager: Laminas\Serializer\AdapterPluginManagerAdapterInterface: Laminas\Serializer\Adapter\AdapterInterfaceAdapterOptions: Laminas\Serializer\Adapter\AdapterOptions| Method signature | Description |
|---|---|
factory(/* ... */) : AdapterInterface |
Create a serializer adapter instance. Arguments are: string|AdapterInterface $adapterName, AdapterOptions|array|Traversable $adapterOptions = null. |
setAdapterPluginManager(AdapterPluginManager $adapters) : void |
Change the adapter plugin manager. |
getAdapterPluginManager() : AdapterPluginManager |
Get the adapter plugin manager. |
resetAdapterPluginManager() : void |
Resets the internal adapter plugin manager. |
setDefaultAdapter(string|AdapterInterface $adapter /* ... */): void |
Change the default adapter. Full argument list: string|AdapterInterface $adapter, AdapterOptions|array|Traversable $adapterOptions = null. |
getDefaultAdapter() : AdapterInterface |
Get the default adapter. |
serialize(mixed $data /* ... */) : string |
Generates a storable representation of a value using the default adapter. Optionally, provide a different adapter via the second argument. Full argument list: mixed $value, string|AdapterInterface $adapter = null, AdapterOptions|array|Traversable $adapterOptions = null. |
unserialize(string $value /* ... */) : mixed |
Creates a PHP value from a stored representation using the default adapter. Optionally, provide a different adapter via the second argument. Full argument list: string $value, string|AdapterInterface|null $adapter = null, AdapterOptions|array|Traversable $adapterOptions = null |
How can I help you explore Laravel packages today?