PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
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 un-serialization 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 un-serialization process.
In PHP, object serialization and un-serialization 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 2015 (c) 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:
herong> \php\php ObjectSerilizationTest.php 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:
Table of Contents
Introduction and Installation of PHP
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
Interface with Operating System
►Introduction of Class and Object
What Is a Superclass and a Subclass
What Is an Overloaded Property
What Is Object Property Iteration
Updating Variables Who Share an Object
Passing Objects as Function Arguments
Integrating PHP with Apache Web Server
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
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS