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:
- Classtestclass
- {
- var$a;
- var$b;
- Functiontestclass ()
- {
- $this- > a = "Thisisa" ;
- $this- > b = "THISISB" ;
- }
- Functiongeta ()
- {
- return$this- > A;
- }
- FUNCTIONGETB ()
- {
- return$this- > b;
- }
- }
- $ obj = Newtestclass ;
- $ Str = Serialize ($obj);
- ECHO$STR;
Output Result:
Let's analyze a string after the serialization of an object.
- O:9: "TestClass": 2:
- {
- S:1: "A"; S:9: "Thisisa";
- 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.
- php
- Classa.inc:
- ClassA
- {
- var$ One = 1 ;
- Functionshow_one ()
- {
- echo$this- > One ;
- }
- }
- page1.php:
- Include ("Classa.inc");
- $ a = Newa ;
- $ s = Serialize ($a);
- Store $s somewhere so page2.php can find
- $ FP = fopen ("Store", "w");
- Fputs ($fp, $s);
- Fclose ($FP);
- page2.php:
- This line is required for normal deserialization
- Include ("Classa.inc");
- $ s = implode ("", @file ("store"));
- $ a = unserialize ($s);
- You can now use the Show_one () function of the $ A object
- $a- > Show_one ();
- ?>
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 ...