What Is Object Serialization?

This section describes what is object serialization - a process of converting an object into a binary string to be saved in a file, stored in database, passed to another system. The string representing the object can be converted back to a new object that has same property values as the original object using the unserialization process

What Is Object Serialization? Object serialization is a process of converting an object into a binary string to be saved in a file, stored in database, passed to another system. The string representing the object can be converted back to a new object that has same property values as the original object using the unserialization process.

In PHP, object serialization and unserialization can be done by calling serialize() and unserialize() functions:

Note that serialize() and unserialize() functions can also be used to serialize and unserialize other types of variables like boolean, integer, float, string, or array.

For example, the following tutorial example shows how to use serialize() function to save an object into a string and unserialize() function to rebuild a copy of the object:

<?php /* ObjectSerilizationTest.php
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
*/
class MyCarQuote {
   public $maker = "BMW";
   public $model = "i8";
   public $year = "2015";
   public $color = "Silver";
   public $engine = "1.5L 3-cylinder";
   public $horsepower = "357 HP";
   private $quote = 123400.0;
   private $cost = 120000.0;
   public function getQuote() {
      return $this->quote;
   }
}

   $car1 = new MyCarQuote();
   $string = serialize($car1);
   echo "String from serialized car1: \n".$string."\n";

   $car2 = unserialize($string);
   echo "Car2 rebuilt from the serialization string: \n";
   foreach($car2 as $key => $value) {
      echo "   $key: $value\n";
   }
   $q = $car2->getQuote();
   echo "   -- quote: \$".intval($q/1000).",".($q%1000).".00\n";

   echo "\$car1 == \$car2: ". (($car1==$car2)?"Yes":"No") ."\n";
   echo "\$car1 === \$car2: ". (($car1===$car2)?"Yes":"No") ."\n";
?>

If you run the above tutorial example, you will get something like this:

String from serialized car1: 
O:10:"MyCarQuote":8:{s:5:"maker";s:3:"BMW";s:5:"model";s:2:"i8";
s:4:"year";s:4:"2015";s:5:"color";s:6:"Silver";s:6:"engine";
s:15:"1.5L 3-cylinder";s:10:"horsepower";s:6:"357 HP";
s:17:" MyCarQuote quote";d:123400;s:16:" MyCarQuote cost";d:120000;}

Car2 rebuilt from the serialization string:
   maker: BMW
   model: i8
   year: 2015
   color: Silver
   engine: 1.5L 3-cylinder
   horsepower: 357 HP
   -- quote: $123,400.00
$car1 == $car2: Yes
$car1 === $car2: No

The output is interesting:

Last update: 2015.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.0

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Configuring and Sending out Emails

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

Introduction of Class and Object

 What Is a Class?

 What Is an Object?

 What Is a Constructor?

 What Is a Static Method?

 What Is a Static Variable?

 What Is a Superclass and a Subclass?

 What Is an Abstract Class?

 What Is an Abstract Method?

 What Is an Interface?

 What Is a Trait?

 What Is an Overloaded Property?

 What Is an Overloaded Method?

 What Is Object Property Iteration?

 What Is Object Cloning?

What Is Object Serialization?

 What Is in an Object Variable?

 Updating Variables Who Share an Object

 Passing Objects as Function Arguments

 Integrating PHP with Apache Web Server

 Outdated Tutorials

 References

 PDF Printing Version