PHP Object Serialization: serialize() & unserialize()

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:

  1. serialize() → Converts an object into a string.
  2. 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.

📖
Important Notes:
  • 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

O:7:”Student”:2:{s:4:”name”;s:4:”John”;s:6:”course”;s:3:”PHP”

Explanation

In this example:

  1. We created a class named Student.
  2. The class contains two properties:
    • name
    • course
  3. 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

John
PHP

Explanation

In this example:

  1. The object $obj is first serialized.
  2. The serialized data is stored in $serializedData.
  3. 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

Laptop 50000

Explanation

Here’s how this example works

  1. We created a Product class with properties name and price.
  2. Then we created an object $product.
  3. 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.

  1. Easy Data Storage
    Objects can be stored in files or databases easily.
  2. Data Transfer
    Serialized objects can be transferred across applications or network systems.
  3. Preserves Object State
    Serialization stores the current state of the object, including its property values.
  4. 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?
Ans: It converts an object into a storable string format.
Q 2: Which function is used for serialization?
Ans: serialize().
Q 3: How to restore a serialized object?
Ans: Using unserialize().
Q 4: Where is serialization commonly used?
Ans: Sessions and caching.
Q 5: Can private properties be serialized?
Ans: Yes.

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?






Related PHP Tutorials