On the usage of php serialize of and unserialize of

  • 2020-06-03 06:11:16
  • OfStack

serialize() and unserialize() as explained in the php manual:
serialize -- Generates a representation of a value
serialize - Produces a representation of a storeable value
unserialize - Creates a PHP from a stored representation
unserialize - Creates the value of PHP from the stored representation
Obviously, the translation of "a stored representation" into a storeable value is still very confusing.
If the language is no longer clear, we can use a concrete example of PHP to learn the use of these two functions

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php
// The statement 1 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);
// Instantiate this class 
$ourfirstdog = new dog("Rover",12,"Lisa and Graham");
// with serialize The function converts this instance to 1 A serialized string 
$dogdisc = serialize($ourfirstdog);
print $dogdisc; //$ourfirstdog  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 put a string  $dogdisc  Store anywhere as  session,cookie, The database ,php file  
// Let's log off the class here 
/*     Restore operation    */
     Put a string here  $dogdisc  Read as if from where you store it  session,cookie, The database ,php file  
// We use it here  unserialize()  Restore the serialized object 
$pet = unserialize($dogdisc); // At this time  $pet  It's already in front  $ourfirstdog  The object 
// Gets the age and name attributes 
$old = $pet->getage();
$name = $pet->getname();
// This class can continue to be used without instantiation at this point , And the properties and values are kept in the pre-serialization state 
print "Our first dog is called $name and is $old days old<br>";
print '<BR>';

The object in the example we can also change to array and other types, the effect is 1 kind!
In fact, serialize() stores the values of variables in PHP such as objects (object), arrays (array), etc. Serialized strings can be stored in other places such as database, Session, Cookie, etc. Serialized operations will not lose the type and structure of these values. Data for these variables can then be passed between PHP pages and even between different PHP programs.
unserialize() converts the serialized string back to the value of PHP.

To quote another paragraph from the PHP manual, it should be easy to understand the meaning of the following words after reading the above examples
To change a serialized string back to the value of PHP, use unserialize(). serialize() can handle any type except resource. You can even serialize() arrays that contain references to themselves. References in your serialize() array/object will also be stored.

When the object is serialized, PHP will try to call the member function of the object with SUCCsleep () before the sequence action. This allows the object to do any cleanup before it is serialized. Similarly, the member function of EACH ___ 63en () is called when using unserialize() to restore the object
unserialize() operates on the serialized variable of single 1, converting it back to the value of PHP. The converted value is returned, and can be integer, float, string, array, or object. Returns FALSE if the string passed is not deserialized.

Related articles: