Simple PHP object serialization learning _ PHP Tutorial

Source: Internet
Author: User
Tags object serialization
Simple PHP object serialization learning. PHP is still quite common, so I studied PHP object serialization and shared it with you here. I hope it will be useful to you. PHP object serialization is also quite common in PHP, so I have studied PHP object serialization and I will share it with you here. I hope it will be useful to you. PHP object serialization is also a common function. It serializes an object into a string and can be saved or transmitted. Let's first look at an example:

 
 
  1. classTestClass
  2. {
  3. var$a;
  4. var$b;
  5. functionTestClass()
  6. {
  7. $this->a="Thisisa";
  8. $this->b="Thisisb";
  9. }
  10. functiongetA()
  11. {
  12. return$this->a;
  13. }
  14. functiongetB()
  15. {
  16. return$this->b;
  17. }
  18. }
  19. $obj=newTestClass;
  20. $str=serialize($obj);
  21. echo$str;

Output result:

 
 
  1. O:9:"TestClass":2:{s:1:"a";s:9:"Thisisa";s:1:"b";s:9:"Thisisb";}

We will analyze the serialized strings of an object.

 
 
  1. O:9:"TestClass":2:
  2. {
  3. s:1:"a";s:9:"Thisisa";
  4. s:1:"b";s:9:"Thisisb";
  5. }

First, let's take a look at the content of the object itself: O: 9: "TestClass": 2: O indicates that this is an object type (object), and then 9 indicates that the object name has checked the concentration, 2 indicates that the object has several attributes. Check the content of the two attributes: s: 1: "a"; s: 9: "Thisisa"; in fact, it is similar to the content of the array. Item 1: s: 1: "a"; describes the property name. The second item s: 9: "Thisisa"; describes the property value. The following attributes are similar. Let's talk about a PHP object serialization application. the following content is in the PHP manual, and the original text is not changed. Serialize () returns a string containing the byte stream representation of any value stored in PHP. Unserialize () can be used to reconstruct the original variable value. Serialization is used to save the object and save all the variables in the object. The functions in the object will not be saved, but only the class name.

To be able to unserialize () an object, you need to define the class of this object. That is, if the object $ A of Class a in page1.php is serialized, A string pointing to Class a is obtained and contains the values of all the variables in $. To serialize the object $ A in Class a in page2.php, the Class A definition must appear in page2.php. This can be implemented as an example. put the definition of Class A in an include file and include this file in both page1.php and page2.php.

 
 
  1. Php
  2. // Classa. inc:
  3. ClassA
  4. {
  5. Var $One=1;
  6. Functionshow_one ()
  7. {
  8. Echo $ this->One;
  9. }
  10. }
  11. // Page1.php:
  12. Include ("classa. inc ");
  13. $A=NewA;
  14. $S=Serialize($ );
  15. // Store $ s somewhere so that page2.php can find
  16. $Fp=Fopen("Store", "w ");
  17. Fputs ($ fp, $ s );
  18. Fclose ($ fp );
  19. // Page2.php:
  20. // This line is required for normal deserialization
  21. Include ("classa. inc ");
  22. $S=Implode("", @ File ("store "));
  23. $A=Unserialize($ S );
  24. // Now you can use the show_one () function of $ a object.
  25. $->Show_one ();
  26. ?>

Bytes. PHP object serialization is also quite common...

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.