Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /modules/zend.serializer.html

Size:20683
Storage flags:no_autoload,compress/gzip (21%)

Introduction to Zend\Serializer — Zend Framework 2 2.4.2 documentation

Introduction to Zend\Serializer

The Zend\Serializer component provides an adapter based interface to simply generate storable representation of PHP types by different facilities, and recover.

For more information what a serializer is read the wikipedia page of Serialization.

Quick Start

Serializing adapters can either be created from the provided Zend\Serializer\Serializer::factory method, or by simply instantiating one of the Zend\Serializer\Adapter\* classes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
use Zend\Serializer\Serializer;

// Via factory:
$serializer = Zend\Serializer\Serializer::factory('PhpSerialize');

// Alternately:
$serializer = new Zend\Serializer\Adapter\PhpSerialize();

// Now $serializer is an instance of Zend\Serializer\Adapter\AdapterInterface,
// specifically Zend\Serializer\Adapter\PhpSerialize

try {
    $serialized = $serializer->serialize($data);
    // now $serialized is a string

    $unserialized = $serializer->unserialize($serialized);
    // now $data == $unserialized
} catch (Zend\Serializer\Exception\ExceptionInterface $e) {
    echo $e;
}

The method serialize() generates a storable string. To regenerate this serialized data you can simply call the method unserialize().

Any time an error is encountered serializing or unserializing, Zend\Serializer will throw a Zend\Serializer\Exception\ExceptionInterface.

Because of an application often uses internally only one serializer it is possible to define and use a default serializer. That serializer will be used by default by other components like Zend\Cache\Storage\Plugin\Serializer.

To use the default serializer you can simply use the static serialization methods of the basic Zend\Serializer\Serializer:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
use Zend\Serializer\Serializer;

try {
    $serialized = Serializer::serialize($data);
    // now $serialized is a string

    $unserialized = Serializer::unserialize($serialized);
    // now $data == $unserialized
} catch (Zend\Serializer\Exception\ExceptionInterface $e) {
    echo $e;
}

Basic configuration Options

To configure a serializer adapter, you can optionally use an instance of Zend\Serializer\Adapter\AdapterOptions, an instance of one of the adapter specific options class, an array or an instance of Traversable. The adapter will convert it into the adapter specific options class instance (if present) or into the basic Zend\Serializer\Adapter\AdapterOptions class instance.

Options can be passed as second argument to the provided Zend\Serializer\Serializer::factory method, using the method setOptions or set as constructor argument.

Available Methods

Each serializer implements the interface Zend\Serializer\Adapter\AdapterInterface.

This interface defines the following methods:

serialize(mixed $value)

Generates a storable representation of a value.

Return type:string
unserialize(string $value)

Creates a PHP value from a stored representation.

Return type:mixed

The basic class Zend\Serializer\Serializer will be used to instantiate the adapters, to configure the factory and to handle static serializing.

It defines the following static methods:

factory(string|Zend\Serializer\Adapter\AdapterInterface $adapterName, Zend\Serializer\Adapter\AdapterOptions|array|Traversable|null $adapterOptions = null)

Create a serializer adapter instance.

Return type:Zend\Serializer\Adapter\AdapterInterface
setAdapterPluginManager(Zend\Serializer\AdapterPluginManager $adapters)

Change the adapter plugin manager.

Return type:void
getAdapterPluginManager()

Get the adapter plugin manager.

Return type:Zend\Serializer\AdapterPluginManager
resetAdapterPluginManager()

Resets the internal adapter plugin manager.

Return type:void
setDefaultAdapter(string|Zend\Serializer\Adapter\AdapterInterface $adapter, Zend\Serializer\Adapter\AdapterOptions|array|Traversable|null $adapterOptions = null)

Change the default adapter.

Return type:void
getDefaultAdapter()

Get the default adapter.

Return type:Zend\Serializer\Adapter\AdapterInterface
serialize(mixed $value, string|Zend\Serializer\Adapter\AdapterInterface|null $adapter = null, Zend\Serializer\Adapter\AdapterOptions|array|Traversable|null $adapterOptions = null)

Generates a storable representation of a value using the default adapter. Optionally different adapter could be provided as second argument.

Return type:string
unserialize(string $value, string|Zend\Serializer\Adapter\AdapterInterface|null $adapter = null, Zend\Serializer\Adapter\AdapterOptions|array|Traversable|null $adapterOptions = null)

Creates a PHP value from a stored representation using the default adapter. Optionally different adapter could be provided as second argument.

Return type:mixed

Table Of Contents

Previous topic

File Upload Handlers

Next topic

Zend\Serializer\Adapter

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Introduction to Zend\Serializer on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.

For more information about the PHK package format: http://phk.tekwire.net