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->name =$in _name;9 $this->age =$in _age;Ten $this->owner =$in _owner; One } A - functionGetage () { - return($this->age * 365); the } - - functionGetOwner () { - return($this->owner); + } - + functiongetname () { A return($this->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->name =$in _name;9 $this->age =$in _age;Ten $this->owner =$in _owner; One } A - functionGetage () { - return($this->age * 365); the } - - functionGetOwner () { - return($this->owner); + } - + functiongetname () { A return($this->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->getage ();Panax Notoginseng $name=$pet->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<br> "; + A?>
The application of PHP serialize unserialize– data serialization and deserialization