PHP object serialization and deserialization PHP object storage and transmission

WBOY
Release: 2016-07-25 08:57:47
Original
1100 people have browsed it
  1. class Person {

  2. private $name;
  3. private $age;
  4. function __construct($name, $age) {
  5. $this->name = $name;
  6. $this->age = $age;
  7. }

  8. function say() {

  9. echo "My name is: ".$this->name."
    ";
  10. echo " My age is: ".$this->age;
  11. }}

  12. $p1 = new Person("张三", 20);

  13. $ p1_string = serialize($p1);//Serialize the object and write it to the file
  14. $fh = fopen("p1.text", "w");
  15. fwrite($fh, $p1_string);
  16. fclose($fh );
  17. ?>

Copy the code

Open the p1.text file and enter the content: O:6:"Person":2:{s:12:" Person name";s:4:"Zhang San";s:11:" Person age";i:20;} But usually the characters generated by the above serialization are not directly parsed.

Second, deserialization:

  1. class Person {

  2. private $name;
  3. private $age;
  4. function __construct($name, $age) {
  5. $this->name = $name ;
  6. $this->age = $age;
  7. }
  8. function say() {
  9. echo "My name is: ".$this->name."
    ";
  10. echo " Me The age is: ".$this->age;
  11. }}

  12. $p2 = unserialize(file_get_contents("p1.text"));

  13. $p2 -> say() ;
  14. ?>
Copy the code

output result: My name is: Zhang San My age is: 20 Tip: Since a serialized object cannot serialize its methods, when unserialize, the current file must contain the corresponding class or require the corresponding class file.

Serialization can only be used with limited users, because files need to be stored or written separately for each user, and the file name must not be repeated. In the case where the user cannot exit the browser normally, there is no guarantee that the file will be deleted. The object is registered as a session variable. When there are a large number of users, you can consider using session to save objects.

For more information about session, please see the article: Simple example of session in php php session operation class (with examples) php session function set Detailed explanation of session expiration setting method in php Examples of session application in php Session usage examples of php session technology php log out session information Cookie and Session usage in php5

Example:

  1. session_start();

  2. class Person {
  3. private $name;
  4. private $age;
  5. function __construct($name, $age) {
  6. $this-> ;name = $name;
  7. $this->age = $age; }
  8. function say() {
  9. echo "My name is: ".$this->name."
    ";
  10. echo " My age is: ".$this->age;
  11. }}

  12. $_SESSION["p1"] = new Person("张三", 20);

  13. ?>

Copy code

Read session:

  1. session_start();

  2. class Person {
  3. private $name;
  4. private $age;
  5. function __construct($name, $age) {
  6. $this-> ;name = $name;
  7. $this->age = $age;
  8. }

  9. function say() {

  10. echo "My name is:".$this->name ."
    ";
  11. echo " My age is: ".$this->age;
  12. }}
  13. $_SESSION["p1"] -> say();
  14. ?>
Copy the code

Output results: My name is: Zhang San My age is: 20 Like serialization, registering an object as a session variable does not save its methods. Therefore, when reading session variables, the current file must contain the corresponding class or require the corresponding class file.



source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!