Simple PHP Object Serialization Learning _php Tutorial

Source: Internet
Author: User
PHP is still more commonly used, so I studied the PHP object serialization, here to share with you, hope to be useful to everyone. PHP object serialization is also a common feature that can serialize an object into a string that can be saved or transmitted. Let's look at an example first:

 
 
  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:

 
  
  

Let's analyze a string after the serialization of an object.

 
  
  
  1. O:9: "TestClass": 2:
  2. {
  3. S:1: "A"; S:9: "Thisisa";
  4. S:1: "B"; S:9: "Thisisb";

First look at the content of the object itself: O:9: "TestClass": 2:o is a description of the object type (object), and then 9 is the name of the object to check the concentration, 2 is to represent the object has several properties. Looking at the contents of the two attributes: s:1: "a"; S:9: "Thisisa"; in fact, it is similar to the contents of an array, the first item: s:1: "A"; is the name of the attribute, the second item S:9: "Thisisa"; is the property value. The following properties are similar. First of all, a PHP object serialization application, the following is the PHP manual, the original text is not changed. Serialize () returns a string containing a byte stream representation of any value that can be stored in PHP. Unserialize () can use this string to reconstruct the original variable value. Using serialization to save an object can hold all the variables in the object. The function in the object is not saved, only the name of the class.

To be able to unserialize () an object, you need to define the class for that object. That is, if you serialize the object $ A of Class A in page1.php, you get a string that points to class A and contains the value of the variable in all $ A. If you want to serialize it in page2.php and rebuild the object $ A of Class A, you must have the definition of Class A in page2.php. This can be done, for example, by placing the definition of Class A in an include file and including 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 ($a);
  15. Store $s somewhere so 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. You can now use the Show_one () function of the $ A object
  25. $a- > Show_one ();
  26. ?>

http://www.bkjia.com/PHPjc/446493.html www.bkjia.com true http://www.bkjia.com/PHPjc/446493.html techarticle PHP is still more commonly used, so I studied the PHP object serialization, here to share with you, hope to be useful to everyone. The serialization of PHP objects is also a more 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.