Serialize () and unserialize () are explained in the PHP manual:
Serialize-generates a storable representation of a value
Serialize-produces a representation of a value that can be stored
Unserialize-creates a PHP value from a stored representation
unserialize-create PHP values from stored representations
Obviously, the interpretation of "a stored representation" translates into a stored value that is still very confusing to the meaning of it.
If the language is not clear, then we can use a specific example of PHP to learn the purpose of these two functions
Copy Code code as follows:
Code highlighting produced by Actipro Codehighlighter (freeware) http://www.CodeHighlighter.com/--><?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> ';
?>
Examples of objects we can also change to other types of arrays, the effect is the same!
In fact, serialize () is the use of variables such as objects (object), arrays (array), and so on in PHP to be stored after the values are serialized. Serialized strings We can store them in other places such as databases, sessions, cookies, etc. The serialized operation does not lose the type and structure of these values. The data for these variables can be passed between PHP pages and even different PHP programs.
and Unserialize () is the conversion of serialized strings back to the value of PHP.
Here is a reference to the PHP manual, read the example above, it should be easy to understand the meaning of the following words
To change the serialized string back to the value of PHP, use Unserialize (). Serialize () can handle any type other than resource. You can even serialize () those that contain arrays that point to their own references. The reference in the array/object you are serialize () will also be stored.
When serializing an object, PHP attempts to invoke the member function __sleep () of the object before the sequence action. This allows the object to do any cleanup operations before being serialized. Similarly, the __wakeup () member function is invoked when the object is recovered using Unserialize ()
Unserialize () operates on a single serialized variable and converts it back to the value of PHP. Returns the value after the conversion, which can be integer, float, string, array, or object. Returns FALSE if the passed string is not solvable.