Example of serialized usage in PHP and example of php serial usage

Source: Internet
Author: User

Example of serialized usage in PHP and example of php serial usage

This example describes the serialization usage in PHP. We will share this with you for your reference. The details are as follows:

Function: serializes objects for storage or transmission. This object is obtained through deserialization.

1. Person. class. php:

<? Php/* Author: shyhero */class Person {// declare a Person class public $ age; private $ name; protected $ sex; public function _ construct ($ age = "", $ name = "", $ sex = "") {$ this-> age = $ age; $ this-> name = $ name; $ this-> sex = $ sex;} public function say () {return $ this-> age. "". $ this-> name. "". $ this-> sex;} function _ sleep () {// specify the member attributes that can be extracted during serialization. There is no parameter, however, when an array $ arr = array ("age", "name"); return $ arr;} function _ wakeup () {// specifies the deserialization, extracted value $ this-> sex = "woman ";}}

2. serialized code

<? Php require (". /Person. class. php "); $ p = new Person (21," du "," man "); // defines the Person Class Object $ pString = serialize ($ p ); // serializes the object file_put_contents (". /file.txt ", $ pString); // save it to a file

3. deserialization code

<? Php require (". /Person. class. php "); // The original class $ pString = file_get_contents (". /file.txt "); // extract the serialized value from the file $ p = unserialize ($ pString); // deserialize var_dump ($ p ); // This $ p is the serialized object. It is used in the same way, but the value in it is changed by me.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.