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.