The explanation of serialize() and unserialize() in the php manual is:
serialize — Generates a storable representation of a value
serialize
— Generate a representation of a storable value
unserialize — Creates a PHP value from a stored representation
unserialize
— Create PHP value from stored representation
Obviously, the explanation of "a stored representation" is still very confusing when translated into a storable value.
If the language can no longer be clearly stated, then we can use a specific PHP example to learn the uses of these two functions.
<?php //Declare a classclass 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); } } //Instantiate this class$ourfirstdog = new dog("Rover",12,"Lisa and Graham"); //Use the serialize function to convert this instance into a serialized string$dogdisc = serialize($ourfirstdog); print $dogdisc; //$ourfirstdog has been serialized to 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 to anywhere such as session, cookie, database, php file ----------------------------------------------------------------------- */ //We will cancel this class hereunset($ourfirstdog); /* Restore operation */ /* ----------------------------------------------------------------------- Here read the string $dogdisc from your storage location such as session, cookie, database, php file ----------------------------------------------------------------------- */ //We use unserialize() to restore the serialized object here$pet = unserialize($dogdisc); //The $pet at this time is already the previous $ourfirstdog object//Get age and name attributes$old = $pet->getage(); $name = $pet->getname(); //This class can be used without instantiation at this time, and the properties and values are kept in the state before serializationprint "Our first dog is called $name and is $old days old<br>"; print '<BR>'; ?>
We can also replace the objects in the examples with arrays and other types, and the effects are the same!
In fact, serialize() is to serialize the values of variables such as objects, arrays, etc. in PHP and store them into strings. We can store serialized strings in other places such as databases, sessions, cookies, etc. The serialization operation will not lose the types and structure of these values. In this way, the data of these variables can be passed on the PHP page or even between different PHP programs.
Unserialize() converts the serialized string back to the PHP value.
Here is another paragraph of the instructions in the PHP manual. After reading the above examples, it should be easy to understand the meaning of the following words.
To convert the serialized string back to the value of PHP, use unserialize(). serialize() can handle any type except resource. It is even possible to serialize() those arrays that contain references to themselves. The references in the array/object you are serialize() will also be stored.
When serializing an object, PHP attempts to call the object's member function __sleep() before the sequence action. This allows the object to do any clearing operations before being serialized. Similarly, when restoring an object using unserialize(), the __wakeup() member function is called
unserialize() operates on a single serialized variable and converts it back to the value of PHP. Returns the value after conversion, which can be integer, float, string, array, or object. If the passed string is not deserialized, FALSE is returned.