PHP object serialization and deserialization _ php instance-php Tutorial

Source: Internet
Author: User
Tags object serialization
We all know that PHP Serialization can include objects and convert variables into continuous bytes data. you can save serialized variables in a file or transmit them over the network, then, the data is deserialized to the original data. This article introduces the serialization and deserialization of PHP Objects. if you are interested, let's learn how to use serialization?

When an object is transmitted over the network, it is saved to the database.

Today we will mention four functions.

All serializing

1. serialize serializes the specified class object $ str = serialize ($ per) // serializes the per object and returns the result to $ str
2. the returned result of unserialize is the object $ per = unserialize ($ str );

Local serialization

3. _ sleep () serializes some attributes of an object.

4. When _ wakeup () is deserialized, the object content is initialized (actually modified ).

We have already introduced the first two usage methods. next we will briefly introduce _ sleep () and _ wakeup () usage methods.

1. if we only want to serialize some attributes of an object, we can use the _ sleep () function.

Add in class definition

Function _ sleep () // only serialize the name and age members in the class {$ arr = new array ('name', 'age '); name and age must be attributes in the class. you can add Return arr according to your actual needs ;}

2. what if we change the name attribute value of per object to "zhang san" in deserialization when class serialization is made?

Function _ wakeup () {This-> name = "James ";}

Describes the serialization of PHP Objects in detail.

We all know that PHP Serialization can include objects and convert variables into continuous bytes data. you can save serialized variables in a file or transmit them over the network, then, the data is deserialized to the original data. This article describes PHP Serialization in detail. PHP can successfully store the attributes and methods of the object defined before the object of the deserialization class. sometimes you may need an object to be executed immediately after the deserialization. For this purpose, PHP will automatically find the _ sleep and _ wakeup methods.

When an object is serialized by PHP, PHP will call the _ sleep method (if any ). after an object is deserialized, PHP calls the _ wakeup method. both methods do not accept parameters. the _ sleep method must return an array containing the attributes to be serialized. PHP will discard other attribute values. If the _ sleep method is not available, PHP will save all attributes. Example 1 shows how to serialize an object using the _ sleep and _ wakeup methods. the Id attribute is a temporary attribute not intended to be retained in the object. the _ sleep method ensures that the id attribute is not included in the serialized object. when a User object is deserialized, the __wakeup method creates a new value for the id attribute. this example is designed to be self-sustaining. in actual development, you may find that objects containing resources (such as or data streams) need these methods.

Listing1 Object serialization

Class User {public $ name; public $ id; function _ construct () {// give user a unique ID to assign a different ID $ this-> id = uniqid ();} function _ sleep () {// do not serialize this-> id is not serialized id return (array ("name");} function _ wakeup () {// give user a unique ID $ this-> id = uniqid () ;}// create an object $ u = new User; $ u-> name = "Leon"; // serialize it serializization note that the id attribute is not serialized, and the id value is discarded $ s = serialize ($ u); // unseriali Ze it deserialization id is assigned $ u2 = unserialize ($ s ); // $ u and $ u2 have different IDs $ u and $ u2 have different IDs print_r ($ u); print_r ($ u2);?>

This article introduces the serialization and deserialization of PHP Objects and hopes to help you.

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.