Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Serialization

Serialization refers to the process of converting a data structure or object into a format that can be easily stored, transmitted, or reconstructed later. In other words, it involves converting complex data, such as objects or data structures, into a linear sequence of bytes or characters that can be saved to a file, sent over a network, or otherwise persisted.

Serialization is commonly used in computer science and software development for various purposes, such as:

  • Data Storage: serialized data can be saved to files or databases, allowing it to be retrieved and reconstructed at a later time.
  • Data Transmission: serialized data can be sent over a network or communication channel, allowing it to be transmitted between different systems or processes.
  • Caching: serialized data can be stored in memory caches, improving the performance of applications by reducing the need to regenerate complex data structures.
  • Remote Procedure Calls, RPC: serialized data can be used to pass function arguments and return values between remote systems or services.
  • Message Queues: serialized data can be placed in message queues to enable communication between different components or microservices.
  • State Persistence: serialized data can represent the state of an application, enabling it to be saved and restored across different sessions.

Common serialization formats include JSON, XML, Protocol Buffers, and MessagePack, among others. Each format has its own characteristics, advantages, and use cases. The choice of serialization format often depends on factors such as data complexity, performance requirements, interoperability, and ease of use.

Deserialization, also known as unserialization, is the reverse process, where the serialized data is converted back into its original form, such as objects or data structures, so that it can be used and manipulated within a program.

There are different ways to implement serialization. The native way is to rely on the serialize() and unserialize(), which, in turn, rely on the __serialize() and __unserialize() magic method. Then, var_export() and require() make another serialization method. WDDX, XML, JSON or YAML all work as serialization, yet they usually are not considered, over speed or performance issues.

<?php

    class Elephpant {
        private const HABITAT = 1;

        function trumpet() {
            // same as \Elephpant::C;
            return self::C;
        }
    }

?>

Documentation

See Also