Serialization of objects in PHP, PHP object Serialization _php Tutorial

Source: Internet
Author: User

Serialization of objects in PHP, serialization of PHP objects


We all know that PHP serialization can convert variables including objects into continuous bytes data, you can put the serialized variables in a file or on the network, and then crossdress the row to revert to the original data. The article here on the PHP serialized into a detailed introduction. The classes that you define before you crossdress the objects of the class, PHP can successfully store the properties and methods of its objects. Sometimes you may need an object to be executed immediately after the crossdress is serialized. For this purpose, PHP will automatically look for __sleep and __wakeup methods.

When an object is serialized by PHP, PHP invokes the __sleep method (if one exists). After crossdress an object, PHP calls the __wakeup method. Both methods do not accept parameters. __sleep method You must return an array that contains the properties that you want to serialize. PHP discards the values of other properties。 If there is no __sleep method, PHP will save all properties.
 Php/** * @Authors Peng--jun * @Email 1098325951@qq.com * @Date 2016-01-23 14:40:38 * @Linkhttp://www.cnblogs.com/xs-yqz/* @version $Id $ ==========================================*/Header ("content-type:text/html; Charset=utf-8"); classperson{Private$name; Private$sex; Private$age; function __construct ($name, $age, $sex) {$ This->name =$name; $ This->age =$age; $ This->sex =$sex; } function say () {echo"My name is:".$ This->name."sex is:".$ This->sex."Age is:".$ This-Age ; } //Add this method to the class and automatically call and return the array function __sleep () {$arr when serialized= Array ("name"," Age");//The members $name and $age in the array will be serialized, and the member $sex will be ignored.      return($arr); //Use the __sleep () method when you must return an array. } //The method is called automatically when crossdress the object, there is no parameter and no return value function __wakeup () {$ This->age = +; //re-assign a value to the $age property in the new object when the object is re-organized }} $person 1=NewPerson ("Zhang San", -,"male"); $person 1_string=Serialize ($person 1); Echo $person 1_string."
";//crossdress the object and automatically calls the __wakeup () method to re-assign the age in the exclusive. $person 2 =unserialize ($person 1_string); $person 2-say ();?>

The result of the output is:

O:6:"person":2: {s:£ º"personname" ; s:6:" Zhang San "; s:One:"personage  "; I:a;} My name: Zhang San Sex: Age :

2. Save the serialized string to a file, read the string from the file, and crossdress the instance.

Header"content-type:text/html; Charset=utf-8"); classperson{Private$name; Private$sex; Private$age; function __construct ($name, $age, $sex) {$ This->name =$name; $ This->age =$age; $ This->sex =$sex; } function say () {echo"My name is:".$ This->name."sex is:".$ This->sex."Age is:".$ This-Age ; }} $person 1=NewPerson ("Zhang San", +,"male"); $person 1_string=Serialize ($person 1); File_put_contents ("file.txt", $person 1_string);

Header"content-type:text/html; Charset=utf-8"); classperson{Private$name; Private$sex; Private$age; function __construct ($name, $age, $sex) {$ This->name =$name; $ This->age =$age; $ This->sex =$sex; } function say () {echo"My name is:".$ This->name."sex is:".$ This->sex."Age is:".$ This-Age ; }} $person 2_string= File_get_contents ("file.txt"); $person 2=unserialize ($person 2_string); //Crossdress re-forming the object $person2. $person 2-say ();?>

http://www.bkjia.com/PHPjc/1094586.html www.bkjia.com true http://www.bkjia.com/PHPjc/1094586.html techarticle Serialization of Objects in PHP, serialization of PHP objects we all know that PHP serialization can be used to convert variables including objects into continuous bytes data, you can serialize the variable exists a ...

  • 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.