The application of PHP serialize unserialize– data serialization and deserialization

Source: Internet
Author: User

Serialization is the preservation of the state of an object (the amount of each property), which is then obtained at the appropriate time.

When you use a class, it's all new!
If serialize data is serialized later into the database medium
When you use it, it's not new.

After deserialization, you can use it directly! The equivalent of new!
Use examples to explain to you!

Such as:

a.php

1<?PHP2 //declaration of a class3 classDog {4     var $name;5     var $age;6     var $owner;7     functionDog$in _name= "Unnamed",$in _age= "0",$in _owner= "Unknown") {8         $this-&gt;name =$in _name;9         $this-&gt;age =$in _age;Ten         $this-&gt;owner =$in _owner; One     } A  -     functionGetage () { -         return($this-&gt;age * 365); the     } -  -     functionGetOwner () { -         return($this-&gt;owner); +     } -  +     functiongetname () { A         return($this-&gt;name); at     } - } - //instantiation of this class - $ourfirstdog=NewDog ("Rover", "Lisa and Graham"); - //Convert This instance into a serialized string using the Serialize function - $dogdisc=Serialize($ourfirstdog); in Print $dogdisc;//The $ourfirstdog has been serialized as a string o:3: "Dog": 3:{s:4: "Name"; s:5: "Rover"; s:3: "Age"; I:12;s:5: "Owner"; s:15: "Lisa and Graham"; - /* to ----------------------------------------------------------------------------------------- + Here you can store the string $dogdisc to anywhere like Session,cookie, database, PHP file - ----------------------------------------------------------------------------------------- the */ *  $ //We hereby unregister this classPanax Notoginseng  - unset($ourfirstdog); the  +?>

b.php

1<?PHP2 //declaration of a class3 classDog {4     var $name;5     var $age;6     var $owner;7     functionDog$in _name= "Unnamed",$in _age= "0",$in _owner= "Unknown") {8         $this-&gt;name =$in _name;9         $this-&gt;age =$in _age;Ten         $this-&gt;owner =$in _owner; One     } A  -     functionGetage () { -         return($this-&gt;age * 365); the     } -  -     functionGetOwner () { -         return($this-&gt;owner); +     } -  +     functiongetname () { A         return($this-&gt;name); at     } - } - /*Restore Operations*/ - /* - ----------------------------------------------------------------------------------------- - Here you will read the string $dogdisc from where you stored it, such as Session,cookie, database, PHP file in ----------------------------------------------------------------------------------------- - */ to  + $dogdisc= ' O:3: "Dog": 3:{s:4: "Name"; s:5: "Rover"; s:3: "Age"; I:12;s:5: "Owner"; s:15: "Lisa and Graham";} '; - //Here we use unserialize () to restore an already serialized object the $pet=unserialize($dogdisc);//the $pet at this point is already the $ourfirstdog object in front of you. * //Get age and name attributes $ $old=$pet-&gt;getage ();Panax Notoginseng $name=$pet-&gt;getname (); - //This class does not need to be instantiated at this time to continue to use, and the properties and values are persisted in the state before serialization the Print"Our first dog is called$nameand is$oldDays Old&lt;br&gt; "; +  A?>

The application of PHP serialize unserialize– data serialization and deserialization

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.