Home Backend Development PHP Tutorial Detailed explanation of serialization and deserialization of PHP objects

Detailed explanation of serialization and deserialization of PHP objects

Jul 29, 2016 am 09:05 AM
name php serialize sleep

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;
}
Copy after login

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="张三";
}
Copy after login

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); 
?> 
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles