SoFunction
Updated on 2025-04-09

Serialized variables and serialized objects in PHP

Serialization is probably about converting some variables into a byte stream of strings, which is easier to transmit and store. Of course, there is nothing to do when transferring and storage. The key is that it can be converted back after becoming a string, and the original data structure can be maintained.

There is a multi-serialization function in PHP: serialize(). This function converts any variable value (except resource variables) into a string. It can save the string to a file, register it as a Session, and even uses curl to simulate GET/POST to transmit variables to achieve the RPC effect.

If you want to convert the serialized variable into the original variable value of PHP, you can use the unserialize() function.

1. Variable serialization

Let's give simple examples to illustrate serialization and its storage format.

Integer:

$var = 23;
echo serialize($var);

Output:

i:23;

Floating point type:

$var = 1.23;

echo serialize($var);

Output:

d:1.229999999999999982236431605997495353221893310546875;

String:

$var = "This is a string";
echo serialize($var);
$var = "I am a variable";
echo serialize($var);

Output:

s:16:"This is a string";
s:8:"I am a variable";

Boolean type:

$var = true;
echo serialize($var);
$var = false;
echo serialize($var);

Output:

b:1;
b:0;

The situation after serialization of the above basic types is very clear. The storage format after serialization is:

Variable type: [Variable length:] Variable value;

The first character represents the variable type, the second one: represents segmentation, the variable length is optional, that is, it is found in the string type, but not other types, and the last one is the variable value, and each serialized value ends with ";".

For example, after our integer number 23 is serialized, it is: i:23, then it has no length, only type and variable values. i represents integer, divided by colon, and the integer value 23 is saved later, including floating point (double byte type). For Boolean, the type is b (boolean). If it is true, the serialized value is 1. If it is false, the value is 0. Character

There will be an additional saved value in the middle of the string value, which saves the length value of the string, such as the string "This is a string", and the generated serialized value is s:16:"This is a string"; s is a string, representing the type, and the 16 in the middle is the length of the string. If it is Chinese, then each Chinese is saved by two characters, such as the string "I am a variable", and the generated serialized value is: s:8:"I am a variable"; which is the length of 8 characters.

Let's focus on the serialization of array variables.

Array variables:

$var = array("abc", "def", "xyz", "123");
echo serialize($var);

Output:

a:4:{i:0;s:3:"abc";i:1;s:3:"def";i:2;s:3:"xyz";i:3;s:3:"123";}

It is the string value obtained by serializing my array $var. Our $var array includes 4 string elements, namely "abc", "def", "xyz", "123". Let's analyze the serialized data. For the sake of simplicity, we list the serialized data into an array style:

a:4:
{
i:0;s:3:"abc";
i:1;s:3:"def";
i:2;s:3:"xyz";
i:3;s:3:"123";
}

This arrangement is clearer, look at the starting string: a:4:{...} First, the first character a saves the variable type of array type, and the second 4 saves the number of array elements, with a total of 4, and then the content of the array element between {}. For example, the first array element: i:0;s:3:"abc"; i represents that the index value type of the current array element is an integer, and the value is 0, the type of the element value is s (of a string), the number is 3, the specific value is "abc", the semicolon ends, and the following array elements are analogous.

Let's take a look at how using strings as element indexes:

$var = array("index1"=>"abc", "index2"=>"def", "index3"=>"xyz", "index4"=>"123");
echo serialize($var);

Output:

a:4:{s:6:"index1";s:3:"abc";s:6:"index2";s:3:"def";s:6:"index3";s:3:"xyz";s:6:"index4";s:3:"123";}

After becoming an array style:

a:4:
{
 s:6:"index1";s:3:"abc";
 s:6:"index2";s:3:"def";
 s:6:"index3";s:3:"xyz";
 s:6:"index4";s:3:"123";
}

In fact, it is not much different from the above, but the initial index becomes the form of saving strings, such as the first element: s:6:"index1"; s:3:"abc"; the first item is the index value: s:6:"index1"; s is the type, 6 is the length of the index string, and "index1" is the index value. The following s:3:"abc"; is the element value, which is easy to understand, so I won't talk about.

From the above, we have a rough understanding of the serialization of basic data types. In fact, we can completely construct our own serialization functions, or expand from this perspective, and develop our own serialization programs to facilitate our variable exchange.

Of course, we can actually use this function to serialize arrays or any other variables into strings, and then simulate the GET/POST function through the curl function, so as to achieve the function of obtaining data from the remote server if the user performs actions.

2. Object serialization

Object serialization is also a relatively common function. It can serialize an object and then turn it into a string, which can be saved or transmitted.

Let's first look at an example:

class TestClass
{
 var $a;
 var $b;

 function TestClass()
 {
$this->a = "This is a";
$this->b = "This is b";
 }

 function getA()
 {
return $this->a;
 }

 function getB()
 {
return $this->b;
 }
}

$obj = new TestClass;
$str = serialize($obj);
echo $str;

Output result:

O:9:"TestClass":2:{s:1:"a";s:9:"This is a";s:1:"b";s:9:"This is b";}

Let's analyze the string after an object is serialized.

O:9:"TestClass":2:
{
 s:1:"a";s:9:"This is a";
 s:1:"b";s:9:"This is b";
}

First look at the content of the object itself: O:9:"TestClass":2:O means that this is an object type (object), then 9 means that the name of the object has concentration, and 2 means that the object has several properties. Looking at the contents of two attributes:

s:1:"a";s:9:"This is a"; is actually similar to the content of the array. The first item: s:1:"a"; describes the attribute name, and the second item: s:9:"This is a"; describes the attribute value. The following properties are similar.

Let’s talk about an application of object serialization first. The following content is in the PHP manual and the original text has not been changed.

serialize() returns a string containing a byte stream representation of any value that can be stored in PHP. unserialize() This string can be used to reconstruct the original variable value. Serialization to save an object can save all variables in an object. Functions in the object will not be saved, only the class name.

To be able to unserialize() an object, you need to define the class of that object. That is, if the object $a of class A in sequence is serialized, a string pointing to class A will be obtained and the values ​​of all variables in range $a will be included. If you want to deserialize it in , and rebuild the object $a of class A, the definition of class A must appear in . This can be implemented, for example, by placing the definition of class A inclusion file and including this file in and both.

<?php
// :
class A
{
 var $one = 1;

 function show_one()
 {
echo $this->one;
 }
}

// :
include("");

$a = new A;
$s = serialize($a);
//Save $s somewhere so that you can find it
$fp = fopen("store", "w");
fputs($fp, $s);
fclose($fp);

// :
// This line is required for normal deserialization
include("");

$s = implode("", @file("store"));
$a = unserialize($s);

// You can now use the show_one() function of the $a object
$a->show_one();
?>

If you are registering objects with a session and using session_register(), these objects are automatically serialized at the end of each PHP page and automatically deserialized in each subsequent page. Basically, it means that once these objects become part of the session, they can appear in any page.

It is strongly recommended to include the definition of the class of these registered objects in all pages, even if these classes are not used in all pages. Without doing so, an object is deserialized but has no definition of its class, it will lose the class associated with it and become an object of stdClass without any available functions at all, which is useless.

So if in the above example $a becomes part of the session by running session_register("a") , it should include files in all pages, not just and .

Of course, serialized objects can actually be applied in many places. Of course, the processing of serialization in PHP 5 is different. Let's take a look at the statement in the manual:

serialize() Checks if there is a function with the magic name __sleep in the class. If so, the function will run before any serialization. It can clear the object and should return an array containing all the variable names in the object that should be serialized.

The purpose of using __sleep is to close any object that may havedatabaseConnect, submit waiting data or perform similar clearing tasks. In addition, this function is also useful if there are very large objects that do not need to be stored completely.

Instead, unserialize() checks for the existence of a function with the magic name __wakeup. If present, this function can rebuild any resources the object may have.

The purpose of using __wakeup is to rebuild any database connections that may be lost in serialization and to handle other reinitialization tasks.