Home Backend Development PHP Tutorial Section 13--Object Serialization--ClassesandObjectsinPHP513_PHP Tutorial

Section 13--Object Serialization--ClassesandObjectsinPHP513_PHP Tutorial

Jul 13, 2016 pm 05:24 PM
object

+------------------------------------------------- ----------------------------------+ | = This article is read by Haohappy> | = Chapter Classes and Objects Notes | = Translation + personal experience | = Please do not reprint to avoid unnecessary trouble, thank you | = Criticisms and corrections are welcome, and I hope to make progress together with all PHP enthusiasts! +--------- -------------------------------------------------- --------------------+ */ Section 13 - Object serialization 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. Then deserialize and restore them to the original data. For classes you define before deserializing the class objects, PHP can successfully store its objects. Properties and methods. Sometimes you may need an object to be executed immediately after deserialization. For such purposes, PHP will automatically look for the __sleep and __wakeup methods. When an object is serialized, PHP will call the __sleep method (if present). After deserializing an object, PHP will call the __wakeup method. Neither method accepts parameters. The __sleep method must return an array containing the properties that need to be serialized. PHP will discard the other The value of the attribute. Without the __sleep method, PHP will save all attributes. Example 6.16 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. __sleep method ensures that the id attribute is not included in the serialized object. When deserializing 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 the resource contains Objects (such as images or data streams) require these methods. Listing 6.16 Object serialization id = uniqid(); } function __sleep() { //do not serialize this->id Do not serialize id return(array( "name")); } function __wakeup() { //give user a unique ID $this->id = uniqid(); } } //create object Create an object $u = new User; $u->name = "Leon"; //serialize it. Note that the id attribute is not serialized during serialization. The value of id is discarded. $s = serialize($u); //unserialize it. The id is reassigned during deserialization. $u2 = unserialize( $s); //$u and $u2 have different IDs $u and $u2 have different IDs print_r($u); print_r($u2); ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532151.htmlTechArticle+----------------------- -------------------------------------------------- ------+ | = This article is read by Haohappy> | = Notes from the Chapter Classes and Objects| = Translation + personal experience...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Convert an array or object to a JSON string using PHP's json_encode() function Convert an array or object to a JSON string using PHP's json_encode() function Nov 03, 2023 pm 03:30 PM

JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Use Python's __contains__() function to define the containment operation of an object Use Python's __contains__() function to define the containment operation of an object Aug 22, 2023 pm 04:23 PM

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

Source code exploration: How are objects called in Python? Source code exploration: How are objects called in Python? May 11, 2023 am 11:46 AM

Wedge We know that objects are created in two main ways, one is through Python/CAPI, and the other is by calling a type object. For instance objects of built-in types, both methods are supported. For example, lists can be created through [] or list(). The former is Python/CAPI and the latter is a calling type object. But for instance objects of custom classes, we can only create them by calling type objects. If an object can be called, then the object is callable, otherwise it is not callable. Determining whether an object is callable depends on whether a method is defined in its corresponding type object. like

What is the difference between arrays and objects in PHP? What is the difference between arrays and objects in PHP? Apr 29, 2024 pm 02:39 PM

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values ​​are passed and object references are passed.

Detailed explanation of 5 loop traversal methods of Javascript objects Detailed explanation of 5 loop traversal methods of Javascript objects Aug 04, 2022 pm 05:28 PM

How to loop through Javascript objects? The following article will introduce five JS object traversal methods in detail, and briefly compare these five methods. I hope it will be helpful to you!

Use Python's __le__() function to define a less than or equal comparison of two objects Use Python's __le__() function to define a less than or equal comparison of two objects Aug 21, 2023 pm 09:29 PM

Title: Using Python's __le__() function to define a less than or equal comparison of two objects In Python, we can define comparison operations between objects by using special methods. One of them is the __le__() function, which is used to define less than or equal comparisons. The __le__() function is a magic method in Python and is a special function used to implement the "less than or equal" operation. When we compare two objects using the less than or equal operator (<=), Python

How do PHP functions return objects? How do PHP functions return objects? Apr 10, 2024 pm 03:18 PM

PHP functions can encapsulate data into a custom structure by returning an object using a return statement followed by an object instance. Syntax: functionget_object():object{}. This allows creating objects with custom properties and methods and processing data in the form of objects.

See all articles