Anatomy of PHP Serialization format_php tutorial

Source: Internet
Author: User
Tags object serialization
For you to understand the PHP serialization format. For those functions that have just come in touch with PHP (starting from PHP3.05) and provide a set of serialization and deserialization functions for saving objects: serialize and unserialize. However, in the PHP Manual, these two letters are for those who have just come into contact.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 format 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.

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, in addition, strings are calculated in bytes (or 8-bit characters). Therefore, it is more appropriate to say that the serialized content in PHP 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, the PHP serialization format programs implemented by other languages basically implement serialization and deserialization of these types. However, there are some problems in the implementation of 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 PHP serialization format of the string, but this type of PHP5 and PHP4 are not supported, these two versions are currently mainstream, so we do not recommend using them for serialization when other languages implement this type, but we 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, in the source code of PHP4, PHP5, and PHP6, the PHP serialization format cannot be found, however, in the source code of the deserialization program of these versions, it is processed, but I haven't figured out what it is. Therefore, we will not explain it more.


Pluphp (starting with PHP 3.05) provides a set of serialization and deserialization functions for saving objects: serialize and unserialize. But in the PHP Manual, these two letters...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.