Detailed serialization and crossdress of PHP objects, detailed serial _php tutorial

Source: Internet
Author: User
Tags object serialization

Detailed serialization and crossdress of PHP objects, detailed serial


When do I use serialization?

When you save a file to a database while the object is being transferred to the network

Today we're going to mention four functions

All serialization

1.serialize (object name) serialize the specified class object $str =serialize ($per)//serialize the Per object and return the result to $str
2.unserialize (the serialized return value) returns the result of the object $per =unserialize ($STR);

Local serialization

3.__sleep () Serialize some of the properties of an object.

4.__wakeup () initializes (actually modifies) the object contents when the crossdress is serialized.

The first two uses we have probably already introduced, next we introduce briefly __sleep () and __wakeup () use method

1. We can use the __sleep () function if we want to serialize only a subset of the properties of an object

Adding in the class definition

Function__sleep ()//Only the name and age members in the serialized class {$arr =new array (' name ', ' age '); name and age must be attributes in the class to increase the return arr according to their actual needs;}

2. If we serialize the class, the value of the per object's Name property is "Jiangtong" when deserializing I want to change to "Zhang San" What to Do

function __wakeup () {this->name= "Zhang San";}

Detailed description of the object PHP serialization

We all know that PHP serialization can convert variables including objects into continuous bytes data, you can put the serialized variables in a file or on the network, and then crossdress the row to revert to the original data. The article here on the PHP serialized into a detailed introduction. The classes that you define before you crossdress the objects of the class, PHP can successfully store the properties and methods of its objects. Sometimes you may need an object to be executed immediately after the crossdress is serialized. For this purpose, PHP will automatically look for __sleep and __wakeup methods.

When an object is serialized by PHP, PHP invokes the __sleep method (if one exists). After crossdress an object, PHP calls the __wakeup method. Both methods do not accept parameters. The __sleep method must return an array containing the properties that need to be serialized. PHP discards the values of other properties. If there is no __sleep method, PHP will save all properties. Example 1 shows how to serialize an object using the __sleep and __wakeup methods. The id attribute is a temporary property that is not intended to be persisted in the object. The __sleep method guarantees that the ID attribute is not included in the serialized object. When crossdress a user object, the __wakeup method establishes the new value of the id attribute. This example is designed to be self-sustaining. In real-world development, you might find that objects that contain resources (like or data streams) require these methods.

Listing1 Object Serialization

The knowledge about the serialization and crossdress of PHP objects is introduced here, and we hope to help you.

Articles you may be interested in:

    • Serialization variables and serialized objects in PHP
    • PHP Object-oriented (16) serialization of objects

http://www.bkjia.com/PHPjc/1095279.html www.bkjia.com true http://www.bkjia.com/PHPjc/1095279.html techarticle Explain the serialization and crossdress of PHP objects, and explain when serialization is used. When you save a file to a database while transferring objects in the network, today we are going to mention four ...

  • 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.