


Detailed explanation of serialization and deserialization of PHP objects
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
