Object Serialization

Ma Mahalakshmi V Updated 14 Nov 2026
6 min read ·Lesson 179 of 256

What Is Serialization?

In simple terms: Serialization turns a value — including an object with all its properties — into a string, so it can be stored in a file, a cache, a session, or a queue, and later turned back into the same value. Unserialization is the reverse: it rebuilds the object from the string.

Technically: serialize() converts a PHP value into PHP's own text format, which records types, class names, and property values. unserialize() recreates the value. Classes can control the process with the magic methods __serialize() and __unserialize() (PHP 7.4+). PHP uses serialization internally, for example to store $_SESSION data.

Where Serialization Is Used

UseExample
SessionsPHP serializes $_SESSION between requests
CachingStoring computed objects in files, APCu, or Redis
QueuesLaravel and Symfony serialize job objects to run them later
SnapshotsSaving the state of an object for later restoration

For exchanging data with other systems (APIs, JavaScript, other languages), use JSON instead — serialization is a PHP-specific format.


serialize() and unserialize()

<?php
class Product
{
    public function __construct(public string $name, public int $price) {}
}

$data = serialize(new Product('Pen', 20));
echo $data . "\n";

$restored = unserialize($data);

var_dump($restored instanceof Product);
echo "{$restored->name}: ₹{$restored->price}\n";

Output

O:7:"Product":2:{s:4:"name";s:3:"Pen";s:5:"price";i:20;}
bool(true)
Pen: ₹20

Reading the Format

PartMeaning
O:7:"Product"An Object of the class with the 7-character name Product
:2:{...}Two properties follow
s:4:"name"A string of length 4: the property name
s:3:"Pen"The value: a string of length 3
i:20An integer value

How It Works

  • serialize() records the class name and every property, including protected and private ones (which are stored with special name prefixes).
  • unserialize() creates an object of the recorded class without calling its constructor and sets the properties directly.
  • The class must be defined (or autoloadable) when unserializing; otherwise PHP creates an __PHP_Incomplete_Class placeholder.

Controlling Serialization: __serialize() and __unserialize()

By default, all properties are serialized exactly as they are. Often that is not ideal: some properties should be excluded (database connections, caches), some should be stored in a simpler form, and stored data may need to survive changes to the class. The two magic methods give full control:

<?php
final class Cart
{
    private array $items = [];
    private DateTimeImmutable $updatedAt;

    public function __construct(DateTimeImmutable $now)
    {
        $this->updatedAt = $now;
    }

    public function add(string $sku, int $quantity): void
    {
        $this->items[$sku] = ($this->items[$sku] ?? 0) + $quantity;
    }

    public function summary(): string
    {
        return array_sum($this->items) . ' items, updated ' . $this->updatedAt->format('d M Y H:i');
    }

    public function __serialize(): array
    {
        return [
            'version' => 2,
            'items' => $this->items,
            'updated' => $this->updatedAt->format(DATE_ATOM),
        ];
    }

    public function __unserialize(array $data): void
    {
        $this->items = $data['items'] ?? [];
        $this->updatedAt = new DateTimeImmutable($data['updated'] ?? 'now');
    }
}

$cart = new Cart(new DateTimeImmutable('2026-10-15 10:00', new DateTimeZone('Asia/Kolkata')));
$cart->add('PEN-01', 3);
$cart->add('NB-A5', 2);

$stored = serialize($cart);
echo $stored . "\n";

$restored = unserialize($stored);
echo $restored->summary() . "\n";

Output

O:4:"Cart":3:{s:7:"version";i:2;s:5:"items";a:2:{s:6:"PEN-01";i:3;s:5:"NB-A5";i:2;}s:7:"updated";s:25:"2026-10-15T10:00:00+05:30";}
5 items, updated 15 Oct 2026 10:00

How It Works

  1. __serialize() returns an array describing the object's state. Only this array is stored.
  2. The date is stored as a simple ISO 8601 string instead of a serialized DateTimeImmutable object.
  3. A version number is included, so future versions of the class can recognize and convert older data.
  4. __unserialize() receives the same array and rebuilds the object. Using ?? defaults keeps it working even if older stored data lacks a key.

Older Mechanisms

MechanismStatus
__sleep() / __wakeup()Older; __sleep() returns property names to keep, __wakeup() restores resources. Superseded by __serialize()/__unserialize(), which take precedence if both exist
Serializable interfaceDeprecated since PHP 8.1 unless the class also implements __serialize()/__unserialize()

What Cannot Be Serialized

<?php
try {
    serialize(fn() => 'hello');
} catch (Exception $e) {
    echo $e->getMessage() . "\n";
}

Output

Serialization of 'Closure' is not allowed
ValueResult
ClosuresException
Anonymous class objectsException
Resources (file handles, sockets)Cannot be meaningfully restored; stored as an integer
Objects such as PDO connectionsException
EnumsSupported (stored as the case reference)

Objects holding such values must exclude them in __serialize() and recreate them after unserialization — for example, reconnecting to the database through a service rather than storing the connection.


Security: Never Unserialize Untrusted Data

unserialize() can create objects of any class available in the application and set their properties to attacker-chosen values. Magic methods such as __wakeup(), __unserialize(), __destruct(), or __toString() of those classes then run with that data. Attackers chain existing classes from frameworks and libraries ("gadget chains") to delete files or even execute code. This is called PHP object injection.

Rules

  1. Never pass user-controlled data (cookies, form fields, request bodies, URLs) to unserialize().
  2. For data from users or other systems, use JSON (json_encode()/json_decode()), which never creates arbitrary objects.
  3. If you must unserialize data that could be tampered with, restrict the allowed classes and sign the data with an HMAC (see $_COOKIE).

The allowed_classes Option

<?php
class Product
{
    public string $name = 'Pen';
}

$data = serialize(new Product());

$safe = unserialize($data, ['allowed_classes' => false]);
echo get_class($safe) . "\n";

$allowed = unserialize($data, ['allowed_classes' => [Product::class]]);
echo get_class($allowed) . "\n";

Output

__PHP_Incomplete_Class
Product

With allowed_classes => false, no real objects are created; any object becomes an inert __PHP_Incomplete_Class. An array of class names permits only those classes. This option reduces the risk, but it is not a substitute for keeping untrusted data away from unserialize().


Serialization vs JSON

serialize()json_encode()
Readable by other languagesNoYes
Preserves PHP classes and typesYesNo (objects become JSON objects; decoding gives arrays or stdClass)
Safe for untrusted inputNoYes (decoding never creates your classes)
Typical useSessions, caches, queues — data your application wrote itselfAPIs, front ends, storage meant to be shared

Common Mistakes

Unserializing User Input

Effect: Object injection vulnerabilities.

Fix: Use JSON for anything that comes from outside.

Serializing Resources and Connections

Effect: Exceptions or broken objects after unserializing.

Fix: Exclude them in __serialize() and recreate them afterwards.

Expecting the Constructor to Run

Effect: Validation or initialization in __construct() is skipped on unserialization.

Fix: Validate in __unserialize().

Changing Classes Without Handling Old Data

Effect: Cached or queued objects from older deployments fail to load.

Fix: Version the serialized data and handle missing keys in __unserialize(), or clear caches during deployment.


Best Practices

  • Serialize only data your own application created and controls.
  • Implement __serialize()/__unserialize() to store stable, minimal, versioned state.
  • Never store connections, closures, or other non-serializable values.
  • Use JSON for data exchange and untrusted input.
  • Restrict allowed_classes whenever unserialization cannot be avoided for less trusted data.

Official reference: https://www.php.net/manual/en/language.oop5.serialization.php

0 Comments

Reviewed before they appear

No comments yet.

PHP
PHP Tutorial Index PHP Home PHP Intro PHP Install PHP Built-in Web Server PHP Syntax PHP Tags PHP and HTML PHP Comments PHP Multiline Comments PHP Variables Variables Scope Static Variables Variable Variables PHP Echo / Print PHP printf / sprintf PHP Data Types PHP Booleans PHP NULL PHP Strings String Functions Modify Strings Concatenate Strings Slicing Strings Escape Characters String Interpolation Heredoc and Nowdoc Search in Strings Multibyte Strings PHP Floats PHP Casting PHP Type Juggling PHP Math PHP Random Numbers PHP Constants PHP Magic Constants PHP Operators Arithmetic Operators Assignment Operators Comparison Operators Increment / Decrement Operators Logical Operators Null Coalescing Operator Spaceship Operator Operator Precedence PHP If...Else...Elseif PHP If PHP If Operators PHP If...Else PHP Shorthand if PHP Nested if PHP Switch PHP Match Alternative Syntax PHP Loops While Loop Do While Loop For Loop Foreach Loop Nested Loops Break Statement Continue Statement PHP Functions User-defined Functions Function Arguments Default Argument Values Pass by Reference Named Arguments Variadic Functions Return Values Type Declarations in Functions Recursive Functions Variable Functions Anonymous Functions Closures Arrow Functions First-class Callable Syntax PHP Arrays Indexed Arrays Associative Arrays Create Arrays Access Array Items Update Array Items Add Array Items Remove Array Items Sorting Arrays Multidimensional Arrays Array Destructuring Array Spread Operator Search in Arrays Merge Arrays array_map / array_filter / array_reduce Array Functions PHP Superglobals $GLOBALS $_SERVER $_REQUEST $_POST $_GET $_FILES $_COOKIE $_SESSION \$\_ENV PHP RegEx RegEx Patterns RegEx Modifiers preg_match preg_replace preg_split PHP RegEx Functions PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail Form Input Sanitizing Escaping Output (htmlspecialchars) Checkbox, Radio and Select Inputs Sticky Forms PHP Form Complete PHP Date and Time Date Formatting DateTime Class Timezones PHP Include Include vs Require PHP File Handling PHP File Open/Read PHP File Create/Write File Delete / Rename / Copy PHP Directories PHP CSV Files PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Callback Functions PHP JSON PHP Exceptions Error Handling Custom Error Handler Error Logging HTTP Headers and Redirects Output Buffering Sending Email PHP cURL Consuming REST APIs PHP Generators PHP What is OOP PHP Classes/Objects The $this Keyword PHP Properties PHP Methods PHP Constructor Constructor Property Promotion PHP Destructor PHP Access Modifiers Readonly Properties PHP Inheritance Method Overriding The final Keyword PHP Class Constants PHP Abstract Classes PHP Interfaces Polymorphism PHP Traits PHP Static Methods PHP Static Properties Late Static Binding Magic Methods Object Cloning Comparing Objects Anonymous Classes PHP Enums PHP Namespaces Autoloading Classes PHP Iterables Iterators Object Serialization Strict Types Union Types Nullable Types Mixed and Never Types Nullsafe Operator PHP Attributes PHP Fibers Readonly Classes Property Hooks (PHP 8.4) Asymmetric Visibility (PHP 8.4) Reflection API SPL Data Structures MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data MySQL Joins with PHP Pagination with PHP PDO Introduction PDO Connect PDO Prepared Statements PDO Fetch Modes PDO Transactions PDO Error Handling PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat Parser PHP DOM Parser PHP XMLReader PHP XMLWriter AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll AJAX with JSON Security Introduction Password Hashing SQL Injection Prevention XSS Prevention CSRF Protection Session Security Secure Cookies File Upload Security Filesystem Security Error Reporting in Production Hiding PHP Encryption with Sodium PHP Configuration (php.ini) PHP Command Line (CLI) Composer PSR-4 Autoloading Installing Extensions (PECL / PIE) Debugging PHP OPcache & Performance Garbage Collection PHP-FPM Migrating to PHP 8 Project: Contact Form with Email Project: Login & Registration System Project: CRUD App with MySQL Project: Image Upload Gallery Project: Shopping Cart with Sessions Project: Simple Blog Project: REST API with PHP
Ask about this post
AI Ask about this post

Ask questions about Object Serialization and get answers drawn from it.

Signed-in readers only.