Home  >  Article  >  Backend Development  >  Detailed explanation of serialization and deserialization of PHP objects

Detailed explanation of serialization and deserialization of PHP objects

WBOY
WBOYOriginal
2016-07-29 09:05:13878browse

When to use serialization?

When transferring objects over the network and saving files to the database

Today we are going to mention four functions

All serialized

1.serialize (object name) String the specified class object Serialization $str=serialize($per) //Serialize the per object and return the result to $str
2.unserialize (return value after serialization) The return result is the object $per=unserialize($str);

Partial serialization

3.__sleep() serializes some attributes of an object.

4. __wakeup() initializes (actually modifies) the object content when deserializing

We have probably introduced the first two methods of use. Next, let’s briefly introduce __sleep() and __wakeup () usage method

1. If we only want to serialize some attributes in an object, we can use the __sleep() function

add

function__sleep()//只序列化类中的name和age成员
{
$arr=new array('name','age'); name和age必须是类中的属性 可以根据自己的实际需要增加
Return arr;
}

2. If we serialize in the class When, the name attribute value of the per object is "Jiang Tong", what should I do if I want to change it to "Zhang San" during deserialization?

function __wakeup()
{
This->name="张三";
}

Introduction to object PHP serialization in detail

We all know PHP serialization can convert variables, including objects, into continuous bytes data. You can store the serialized variables in a file or transmit them over the network, and then deserialize them back to the original data. This article gives you a detailed introduction to PHP serialization. PHP can successfully store the properties and methods of a class that you define before deserializing its object. Sometimes you may need an object to be executed immediately after deserializing it. For such purposes, PHP automatically looks for the __sleep and __wakeup methods.

When an object is serialized by PHP, PHP will call the __sleep method (if it exists). After deserializing an object, PHP will call the __wakeup method. Neither method accepts parameters. The __sleep method An array must be returned containing the properties to be serialized. PHP will discard the values ​​of other properties. Without the __sleep method, PHP will save all attributes. Example 1 shows how to use the __sleep and __wakeup methods to serialize an object. The Id attribute is a temporary attribute that is not intended to be retained in the object. The __sleep method guarantees that the id attribute is not included in the serialized object. When reversed To serialize a User object, the __wakeup method establishes 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 images or data streams) require these methods.

Listing1 Object serialization

class User 
{ 
public $name; 
public $id; 
function __construct() 
{ 
//give user a unique ID 赋予一个不同的ID 
$this->id = uniqid(); 
} 
function __sleep() 
{ 
//do not serialize this->id 不串行化id 
return(array("name")); 
} 
function __wakeup() 
{ 
//give user a unique ID 
$this->id = uniqid(); 
} 
} 
//create object 建立一个对象 
$u = new User; 
$u->name = "Leon"; 
//serialize it 串行化 注意不串行化id属性,id的值被抛弃 
$s = serialize($u); 
//unserialize it 反串行化 id被重新赋值 
$u2 = unserialize($s); 
//$u and $u2 have different IDs $u和$u2有不同的ID 
print_r($u); 
print_r($u2); 
?> 

The relevant knowledge about serialization and deserialization of PHP objects is introduced here. I hope it will be helpful to you.

The above has introduced a detailed explanation of the serialization and deserialization of PHP objects, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn