1. Introduction
In PHP, Object Serialization is the process of converting an object into a string representation so that it can be stored, transferred, or saved for later use. Later, this string can be converted back into the original object.
Note: In PHP, serialization is done using the serialize() function, and the reverse process is done using the unserialize() function.
In simple words:
- serialize() → Converts an object into a string.
- unserialize() → Converts the string back into the original object.
Serialization is very useful when developers want to save object data in files, sessions, cookies, or databases.
- When serializing an object, PHP stores only the object’s property values.
- The methods of the object are not serialized.
- The serialized data is stored in a byte-stream format, which is a structured string representation.
2. Syntax
The basic syntax of object serialization in PHP involves two functions:
1. Serialize Function
serialize($object);
This function converts an object into a serialized string.
2. Unserialize Function
unserialize($string);
This function converts the serialized string back into an object.
Basic Structure
$string = serialize($object);
$newObject = unserialize($string);
Here:
- $object is converted into a string.
- The string is stored or transferred.
- Later it is converted back into an object.
3. Simple Example of Object Serialization
Let us understand serialization with a simple example.
Example
class Student {
public $name = "John";
public $course = "PHP";
}
$obj = new Student();
$serializedData = serialize($obj);
echo $serializedData;
Output
Explanation
In this example:
- We created a class named Student.
- The class contains two properties:
- name
- course
- Then we created an object:
$obj = new Student();
4. Next, we converted the object into a string using:
$serializedData = serialize($obj);
The output is a structured string containing object information.
This string includes:
- Class name
- Property names
- Property values
This format allows PHP to reconstruct the object later.
4. Converting Serialized String Back to Object
Now let us convert the serialized string back into an object using the unserialize() function.
Example
class Student {
public $name = "John";
public $course = "PHP";
}
$obj = new Student();
$serializedData = serialize($obj);
$newObject = unserialize($serializedData);
echo $newObject->name;
echo "
";
echo $newObject->course;
Output
PHP
Explanation
In this example:
- The object $obj is first serialized.
- The serialized data is stored in $serializedData.
- Then we convert it back into an object using:
$newObject = unserialize($serializedData);
Now $newObject behaves exactly like the original object.
We can access its properties:
echo $newObject->name;
This demonstrates how serialization and unserialization work together.
5. Real-World Example
A common real-world use case of serialization is storing object data in a file or database.
Example: Storing an Object in a File
class Product {
public $name = "Laptop";
public $price = 50000;
}
$product = new Product();
$data = serialize($product);
file_put_contents("product.txt", $data);
$storedData = file_get_contents("product.txt");
$newProduct = unserialize($storedData);
echo $newProduct->name;
echo "
";
echo $newProduct->price;
Output
Explanation
Here’s how this example works
- We created a Product class with properties name and price.
- Then we created an object $product.
- The object is serialized using:
$data = serialize($product);
4. The serialized data is saved into a file:
file_put_contents("product.txt", $data);
5. Later we read the stored data using:
$storedData = file_get_contents("product.txt");
6. Finally, we convert the string back into an object:
$newProduct = unserialize($storedData);
This allows developers to store and retrieve objects easily.
6. Situations Where Serialization is Used
Object serialization is widely used in real-world PHP applications.
1. Passing Objects in Web Forms
Sometimes object data needs to be passed through hidden form fields. Serialization allows converting the object into a string that can be sent through forms.
2. Passing Objects in URL Query Strings
Objects can be serialized and added to a URL parameter to transfer structured data between pages.
Example:
example.php?data=serialized_string
3. Storing Objects in Files
Serialized objects can be stored in text files and restored later.
4. Saving Objects in Databases
Sometimes developers store serialized objects in a single database column.
5. PHP Sessions
PHP internally uses serialization when storing objects inside sessions.
Example:
$_SESSION['user'] = serialize($object);
7. Advantages of Object Serialization
Object serialization provides several advantages.
- Easy Data Storage
Objects can be stored in files or databases easily. - Data Transfer
Serialized objects can be transferred across applications or network systems. - Preserves Object State
Serialization stores the current state of the object, including its property values. - Useful for Sessions
PHP uses serialization internally to store complex data in sessions.
9. Conclusion
PHP Object Serialization is a powerful feature that allows developers to convert objects into a storable string format and later reconstruct them.
Using the serialize() and unserialize() functions, developers can easily store objects in files, databases, sessions, or transfer them between systems.
Note: It is important to remember that serialization stores only the state of the object (its properties) and not the methods. The serialized data is stored in byte-stream format, which allows PHP to rebuild the object when needed.
PHP Object Serialization – Interview Questions
Q 1: What is object serialization in PHP?
Q 2: Which function is used for serialization?
Q 3: How to restore a serialized object?
Q 4: Where is serialization commonly used?
Q 5: Can private properties be serialized?
PHP Object Serialization – Objective Questions (MCQs)
Q1. Which function serializes a PHP object?
Q2. unserialize() is used to ______.
Q3. Serialized objects are stored as ______.
Q4. Serialization is useful for ______.
Q5. Which method is called automatically during unserialization?