Adapters

laminas-serializer adapters handle serialization to and deserialization from specific representations.

Each adapter has its own strengths. In some cases, not every PHP datatype (e.g., objects) can be converted to a string representation. In most such cases, the type will be converted to a similar type that is serializable.

As an example, PHP objects will often be cast to arrays. If this fails, a Laminas\Serializer\Exception\ExceptionInterface will be thrown.

The PhpSerialize Adapter

The Laminas\Serializer\Adapter\PhpSerialize adapter uses the built-in serialize()/unserialize() functions, and is a good default adapter choice.

Available Options

Option Data Type Default Value Description
allowed_classes array or bool true The allowed classes for unserialize(), see unserialize() for more information.

The IgBinary Adapter

Igbinary was originally released by Sulake Dynamoid Oy and since 2011-03-14 moved to PECL and maintained by Pierre Joye. It's a drop-in replacement for the standard PHP serializer. Instead of using a costly textual representation, igbinary stores PHP data structures in a compact binary form. Savings are significant when using memcached or similar memory based storages for serialized data.

You need the igbinary PHP extension installed on your system in order to use this adapter.

There are no configurable options for this adapter.

The Json Adapter

The JSON provides serialization and deserialization using PHP’s native JSON functions: json_encode() and json_decode()

During serialization, data is converted to a JSON string using json_encode(). During deserialization, the JSON string is converted back to PHP data structures using json_decode().

Available Options

Option Data Type Default Value Description
assoc_array boolean true Whether to decode JSON into associative arrays.

The PhpCode Adapter

The Laminas\Serializer\Adapter\PhpCode adapter generates a parsable PHP code representation using var_export(). To restore, the data will be executed using eval.

There are no configuration options for this adapter.

Unserializing Objects

Objects will be serialized using the __set_state magic method. If the class doesn't implement this method, a fatal error will occur during execution.

Uses eval()

The PhpCode adapter utilizes eval() to unserialize. This introduces both a performance and potential security issue as a new process will be executed. Typically, you should use the PhpSerialize adapter unless you require human-readability of the serialized data.