PHP: Serialization & Unserialization explanation, code & example
Hello Folks!
When I was new to PHP 5 Object Oriented Programming (OOP), the serialization and un-serialization issues were not clear to me and I used to get confused with them often. Nowadays I can play with them easily and I will try to share my knowledge with you so that you can kill your confusion on these topics.
OK. Let us start!
What is Serialization?
This process makes a storable representation of a value that is useful for storing or passing PHP values around without losing their type and structure.
Remember the “__sleep” function in case of Serialization
Before starting your serialization process, PHP will execute the __sleep function automatically. This is a magic function or method.
What is Unserialization?
This process takes a single serialized variable and converts it back into a PHP value.
Remember the “__wakeup” function in case of Unserialization
Before starting your unserialization process, PHP will execute the __wakeup function automatically. This is a magic function or method.
What can you Serialize and Unserialize?
Many things as such
- Variables (Integer, Float, Real, String etc.)
- Arrays
- Objects etc.
What cannot you Serialize and Unserialize?
Only one type
- Resource-type
A simple example of Serialization and Unserialization
{code type=PHP}
require_once "class.serialize.php";
$myObject = new SerializationTest();
$myObject->counter = 999;
$myObject->myName = ‘Random Visitor’;
$myObject->myAge = 59;
$myString = ‘I am a simple line.’;
$myArray = array();
$myArray[0] = ‘One’;
$myArray[1] = ‘Two’;
$myArray[2] = ‘Three’;
echo $myString . ‘
‘;
var_dump($myArray);
echo ‘
‘;
echo ‘$myObject->counter: ‘ . $myObject->counter . ‘
‘;
echo ‘$myObject->myName: ‘ . $myObject->myName . ‘
‘;
echo ‘$myObject->myAge: ‘ . $myObject->myAge . ‘
‘;
var_dump($myObject);
$mySerializedString = serialize($myString);
$mySerializedArray = serialize($myArray);
$mySerializedObject = serialize($myObject);
$myString = ‘I have changed!’;
$myArray[0] = ‘New 1′;
$myArray[1] = ‘New 2′;
$myArray[2] = ‘New 3′;
echo ‘
‘;
echo $myString . ‘
‘;
var_dump($myArray);
echo ‘
‘;
$myUnserializedString = unserialize($mySerializedString);
$myUnserializedArray = unserialize($mySerializedArray);
$myUnserializedObject = unserialize($mySerializedObject);
echo ‘
‘;
echo $myUnserializedString . ‘
‘;
var_dump($myUnserializedArray);
echo ‘
‘;
echo ‘$myUnserializedObject->counter: ‘ . $myUnserializedObject->counter . ‘
‘;
echo ‘$myUnserializedObject->myName: ‘ . $myUnserializedObject->myName . ‘
‘;
echo ‘$myUnserializedObject->myAge: ‘ . $myUnserializedObject->myAge . ‘
‘;
var_dump($myUnserializedObject);
?>
{/code}
And here is the related class:
{code type=PHP}
class SerializationTest
{
public $counter;
public $myName;
public $myAge;
function __construct()
{
$this->counter = 100;
$this->myName = ‘Tanzilo Insido’;
$this->myAge = 28;
}
function __sleep()
{
echo “
__sleep()
“;
$this->counter = $this->counter + 1;
echo ‘Serialization Process Started!’ . ‘
‘;
echo ‘$this->counter: ‘ . $this->counter . ‘
‘;
return array(myString);
}
function __wakeup()
{
echo “
__wakeup()
“;
$this->counter = $this->counter – 1;
echo ‘UnSerialization Process Started!’ . ‘
‘;
echo ‘$this->counter: ‘ . $this->counter . ‘
‘;
}
}
?>
{/code}
Explanation of the example
If you notice the A to Z of the above code, you will find that we have a $myString variable, $myArray array and $myObject object. First of all, we have set values to them and then printed them all. They all perform as expected. Then we change all the values. Next when we serialize and unserialize the values, our original dara structure returns! And that is what we wanted.
Here is the result of the above code:
An interesting thing to note:
I would like to draw your attention to the fact that using __sleep() method, you can change the values of your declared and initialized variables. But you cannot change them by using __wakeup() function. For example, see the following code:
Now we serialize 3 (three) times and unserialize 2 (two) times.
{code type=PHP}
require_once "class.serialize.php";
$myObject = new SerializationTest();
echo '
Start Value:
‘;
echo ‘$this->counter: ‘ . $myObject->counter . ‘
‘;
echo ‘$this->myString: ‘ . $myObject->myString;
$serializeData_1 = serialize($myObject);
$serializeData_2 = serialize($myObject);
$serializeData_3 = serialize($myObject);
$unSerializeData_1 = unserialize($serializeData_1);
$unSerializeData_2 = unserialize($serializeData_2);
?>
{/code}
And here is the result if we serialize 3 (three) times and unserialize 2 (two) times.
Although we initialized $this->counter variable in the __construct() function, it is not used or has no effect in __wakeup() function. But it works properly in __sleep() or serialize() process. But what is the reason? The explanation is simple. The __wakeup() is part of the process when your data is retrieved from memory. Another thing is – did you notice the array return in the __sleep() function? Without returning this, the process often does not work. So, please remember to keep in your code.
You can download these small pieces of code files from here:
http://www.tanzilo.com/demo/code/serialize/serialize.zip
Thank you for reading.






