PHP (beginning with PHP3.05) provides a set of serialization and deserialization functions for saving objects: serialize and unserialize. However, in the PHP manual, the description of these two functions is limited to how to use them, but the format of serialized results is not described. Therefore
1. Preface
PHP (starting with PHP 3.05) provides a set of serialization and deserialization functions for saving objects: serialize and unserialize. However, in the PHP manual, the description of these two functions is limited to how to use them, but the format of serialized results is not described. Therefore, it is troublesome to implement PHP Serialization in other languages. Although some PHP Serialization programs implemented in other languages have been collected before, these implementations are incomplete. errors may occur when serialization or deserialization of some complex objects. So I decided to write a detailed document on the PHP serialization format (that is, this document) to provide a complete reference when writing php Serialization programs implemented by other languages. The content written in this article is obtained by writing a program to test and read the PHP source code. Therefore, I cannot guarantee that all content is correct, however, I will try my best to ensure the correctness of the content I have written. if I am not quite clear about it, I will clearly point out in this article and hope that you can supplement and improve it.
2. Overview
PHP serialized content is a simple text format, but is sensitive to uppercase and lowercase letters and white spaces (spaces, carriage returns, line breaks, etc.), and strings are in bytes (or 8 characters) therefore, it is more appropriate to say that the content after PHP Serialization is in the byte stream format. Therefore, when implemented in other languages, if the strings in the implemented language are not in the byte storage format, but in the Unicode storage format, the serialized content is not suitable for storing as strings, instead, it should be saved as a byte stream object or byte array. Otherwise, an error will occur during data exchange with PHP.
PHP identifies different types of data with different letters. The Using Serialized PHP with Yahoo! provided by Yahoo! All letters and meanings are given in Web Services:
A-array
B-boolean
D-double
I-integer
O-common object
R-reference
S-string
C-custom object
O-class
N-null
R-pointer reference
U-unicode string |
N represents NULL, while B, d, I, and s represent four scalar types, currently, PHP Serialization programs implemented by other languages basically implement serialization and deserialization of these types, but some implementations have problems with s (string.
A and O are the most commonly used composite types. most other languages implement the serialization and deserialization of a, but O only implements the object serialization format in PHP4, PHP 5 does not support extended object serialization formats.
R and R indicate object references and pointer references respectively. These two are also useful. data with these two identifiers will be generated during serialization of complicated arrays and objects, the two labels are described in detail later. Currently, no other language is available for these two labels.
C is introduced in PHP5, which indicates the custom object serialization method, although this is not necessary for other languages, because it is rarely used, but it will be explained in detail later.
U is introduced in PHP6, which represents a Unicode encoded string. PHP6 provides Unicode-based string storage capabilities, so it provides the serialized string format, but this type of PHP5 and PHP4 are not supported, and these two versions are currently mainstream, therefore, when other languages implement this type, it is not recommended to use it for serialization, but it can implement its deserialization process. I will also describe its format later.
There is another o, which is the only data type that I have not figured out. This identifier is introduced in PHP3 to serialize objects, but it is replaced by O after PHP4. In the source code of PHP3, we can see that the serialization and deserialization of o are basically the same as that of array. However, its shadow cannot be found in the serialization part of the source code of PHP4, PHP5, and PHP6, but it is processed in the source code of the deserialization program of these versions, but I still haven't figured out what it is. Therefore, we will not explain it more.