Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Zend\Serializer – Serialize Your Data

Posted in Teknologi, Tutorial PHP, Zend Framework 2 by samsonasik on August 6, 2012

Serialization is the process of converting a data structure or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and “resurrected” later in the same or another computer environment.

Zend Framework 2 Serializer component can serialize object with 6 Adapter :
1. IgBinary
2. Json
3. PhpCode
4. PhpSerialize
5. PythonPickle
6. Wddx

For example, we will serialize a fake object :

 $x = new \StdClass();
 $x->name = 'toni';
 $x->address = 'Pamekar Street';

So, we need Zend\Serializer Component, and call the adapter :

$serializer =  \Zend\Serializer\Serializer::factory('phpserialize');
$y  =  $serializer->serialize($x); //<~ serialized !
echo $y; 
//~> will display O:8:"stdClass":2:{s:4:"name";s:4:"toni";s:7:"address";s:13:"jalan pamekar";}
//the string can be save into your database table or file or memory buffer🙂

How about unserialize :

$obj = $serializer->unserialize($y);
echo $obj->name;

Done !

References :
1. http://en.wikipedia.org/wiki/Serialization

3 Responses

Subscribe to comments with RSS.

  1. […] Es folgen einige gesammelte Beiträge zum Zend Framework 2. Das besondere, sie stammen alles aus dem selben Blog: ZendSerializer – Serialize Your Data […]

  2. alcalyn said, on August 1, 2016 at 3:56 pm

    Uninteresting, it only calls the php serialize function. Nothing useful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: