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:
- 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:
- 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.
- O:9:"TestClass":2:
- {
- s:1:"a";s:9:"Thisisa";
- s:1:"b";s:9:"Thisisb";
- }
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.
- Php
- // Classa. inc:
- ClassA
- {
- Var $One=1;
-
- Functionshow_one ()
- {
- Echo $ this->One;
- }
- }
-
- // Page1.php:
- Include ("classa. inc ");
-
- $A=NewA;
- $S=Serialize($ );
- // Store $ s somewhere so that 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 );
-
- // Now you can use the show_one () function of $ a object.
- $->Show_one ();
- ?>
Bytes. PHP object serialization is also quite common...