Serialize () and unserialize() are explained in the PHP manual:
Serialize -generates a storable representation of a value
Serialize -represents a value that can be stored
unserialize -creates A PHP value from a stored representation
unserialize-Create a PHP value from a stored representation
<?php
Declaring a class
Class Dog {
var $name;
var $age;
var $owner;
Function Dog ($in _name = "Unnamed", $in _age = "0", $in _owner = "Unknown") {
$this-> name = $in _name;
$this-> age = $in _age;
$this-> owner = $in _owner;
}
function Getage () {
Return ($this-> age * 365);
}
function GetOwner () {
Return ($this-> owner);
}
function GetName () {
Return ($this-> name);
}
}
Instantiation of this class
$ourfirstdog = new Dog ("Rover", "Lisa and Graham");
Converts this instance to a serialized string using the Serialize function
$dogdisc = serialize ($ourfirstdog);
Print $dogdisc; $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";
print ' <BR> ';
/*
-----------------------------------------------------------------------------------------
Here you can store the string $dogdisc anywhere such as Session,cookie, database, PHP file
-----------------------------------------------------------------------------------------
*/
We're canceling this class here.
Unset ($ourfirstdog);
/* Restore Operation * *
/*
-----------------------------------------------------------------------------------------
Here to read the string $dogdisc from where you store it like Session,cookie, database, PHP file
-----------------------------------------------------------------------------------------
*/
We're here to restore an object that has been serialized with Unserialize ()
$pet = Unserialize ($dogdisc); The $pet at this point is already $ourfirstdog object in front of you.
Get age and Name attributes
$old = $pet-> getage ();
$name = $pet-> getname ();
This class does not need to be instantiated at this time to continue to use, and both the property and the value remain in the state before the serialization
Print "Our A-dog is called $name and are $old days old<br>";
print ' <BR> ';
?>