Serialization is probably about converting some variables into a byte stream of strings, which makes it easier to transmit and store. Of course, there is nothing to do with transmission and storage. The key is that it can be converted back into string form and the original data structure can be maintained.
There are many serialization functions in PHP: serialize(). This function converts any variable value (except resource variables) into the form of a string. The string can be saved to a file, or registered as a Session, or even Use curl to simulate GET/POST to transfer variables to achieve the effect of RPC.
If you want to convert serialized variables into PHP original variable values, you can use the unserialize() function.
1. Variable serialization
Let’s take a simple example to illustrate serialization and its storage format.
Integer type:
$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;
that is, the first character represents the variable type, and the second: Represents splitting. The variable length is optional, that is, it is available in the string type but not in other types. The last one is the variable value. 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 value, i represents integer, separated by colon, and the integer value 23 is saved later, including floating point type (double) Byte type) is the same. For Boolean, the type is b (boolean). If it is true, the serialized value is 1. If it is false, the value is 0.
For
string
values, there will be an additional saved value in the middle, which saves the length value of the string, such as the string "This is a string", then the generated serialized value is s:16:"This is a string"; s is a string, representing the type. The 16 in the middle is the length of the string. If it is Chinese, then each Chinese character is stored in two characters. For example, the string "I am a variable", the generated serialized value is :s:8:"I am a variable"; It is 8 characters in length.Next we focus on serialization of array variables.
Array variable:
$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";}
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 simplicity, we list the serialized data in an array format:
a:4:
{
i:0;s:3:"abc";
i:1;s:3:"def";
i:2;s:3:"xyz";
i:3;s:3:"123";
}
The arrangement is clearer. Look at the starting string: a:4:{...} First, the first character a saves the variable type, which is the array (array) type, and the second 4 saves the array elements. The number of , there are 4 in total, and then the contents of the array elements 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 integer, and the value is 0, and the element value type is s (string), The number is 3, the specific value is "abc", ends with a semicolon, and so on for the following array elements.
Let's take a look at what happens if we use 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 changing to 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, except that the starting index changes to the form of saving strings. For example, 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 value of the index. The following s:3:"abc"; is the element value. This is easy to understand, so I won't go into details.
From the above, we have a general understanding of the serialization of basic data types. In fact, we can construct our own serialization function, or expand from this perspective and develop our own serialization program to facilitate our variable exchange.
Of course, in fact, we can also use this function to serialize an array or any other variable into a string, and then use the curl function to simulate the GET/POST function to obtain data from the remote server without the user performing actions.
2. Object serialization
Object serialization is also a relatively common function. It can serialize an object and turn it into a string, which can be saved or transmitted.
Let's look at an example first:
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 serialization of an object.
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 indicates that this is an object type (object), then 9 represents the name of the object, and 2 represents how many objects there are property. Looking at the contents of the two attributes:
s:1:"a";s:9:"This is a"; In fact, it is similar to the contents of the array. The first item: s:1:"a"; is the description attribute name. , the second item s:9: "This is a"; describes the attribute value. The following properties are similar.
Let me first talk about an application of object serialization. The following content is from 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() can use this string to reconstruct the original variable value. Saving an object using serialization saves all variables in the object. The functions in the object are not saved, only the name of the class.
To be able to unserialize() an object, the object's class needs to be defined. That is, if you serialize the object $a of class A in page1.php, you will get a string pointing to class A and containing the values of all variables in $a. If you want to deserialize it in page2.php and reconstruct the object $a of class A, the definition of class A must appear in page2.php. This can be achieved, for example, by placing the definition of class A in an include file and including this file in both page1.php and page2.php.
<?php
// classa.inc:
class A
{
var $one = 1;
function show_one()
{
echo $this->one;
}
}
// page1.php:
include("classa.inc");
$a = new A;
$s = serialize($a);
// Store $s somewhere so page2.php can find it
$fp = fopen("store", "w");
fputs($fp, $s);
fclose($fp);
// page2.php:
// This line is needed for normal deserialization
include("classa.inc");
$s = implode("", @file("store"));
$a = unserialize($s);
// Now you can use the show_one() function of the $a object
$a->show_one();
?>
If you are using a session and use session_register() to register objects, these objects will be automatically serialized at the end of each PHP page, and automatically deserialized in each subsequent page. Basically this means that once these objects become part of a session, they can appear on any page.
It is strongly recommended that all pages include class definitions for these registered objects, even if these classes are not used in all pages. If this is not done, and an object is deserialized but does not have its class defined, it will lose its associated class and become an object of stdClass without any available functions at all, which is very useless.
So if $a becomes part of the session by running session_register("a") in the above example, the classa.inc file should be included in all pages, not just page1.php and page2.php.
Of course, serialized objects can actually be applied in many places. Of course, serialization is handled differently in PHP 5. Let's take a look at what the manual says:
serialize() checks whether there is a function with the magic name __sleep in the class. If so, the function will run before any serialization. It clears the object and should return an array containing the names of all variables in the object that should be serialized.
The purpose of using __sleep is to close any database connections the object may have, submit pending data, or perform similar cleanup tasks. In addition, this function is also useful if you have very large objects that do not need to be stored completely.
Conversely, unserialize() checks for the existence of a function with the magic name __wakeup. This function can reconstruct any resources the object may have, if present.
The purpose of using __wakeup is to reestablish any database connections that may have been lost during serialization and to handle other reinitialization tasks.