When to use serialization?
When transmitting objects over the network and saving files to the database
Today we are going to mention four functions
Serialize all
1.serialize(object name) Serialize the specified class object $str=serialize($per) //Serialize the per object and return the result to $str
2.unserialize (return value after serialization) The return result is an 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 already introduced the usage of the first two. Next, we will briefly introduce the usage of __sleep() and __wakeup()
1. If we only want to serialize some attributes in an object, we can use the __sleep() function
Add
in the class definitionfunction__sleep()//只序列化类中的name和age成员 { $arr=new array('name','age'); name和age必须是类中的属性 可以根据自己的实际需要增加 Return arr; }
2. What if when we serialize the class, the name attribute value of the per object is "Jiang Tong" and I want to change it to "Zhang San" during deserialization
function __wakeup() { This->name="张三"; }
Detailed introduction to object PHP serialization
We all know that 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 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. __sleep The method must return an array 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); ?>
This concludes the introduction to the knowledge about serialization and deserialization of PHP objects. I hope it will be helpful to you.