Incomplete Study of php serialize of and unserialize of

  • 2021-08-16 23:18:10
  • OfStack

serialize () and unserialize () are explained in the php manual as follows:

serialize-Generates a storable representation of a value

serialize --Generates a representation of 1 storable value

unserialize-Creates a PHP value from a stored representation

unserialize Create the value of PHP from the stored representation

Obviously, the interpretation of "a stored representation" is still very puzzling after being translated into a storable value.

If the language is no longer clear, we can learn the purpose of these two functions with a specific example of PHP


<?php
// Declaration 1 Category 
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");
// Use serialize Function to convert this instance to a 1 Serialized strings 
$dogdisc = serialize($ourfirstdog);
print $dogdisc; //$ourfirstdog  Has been serialized to 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 the string  $dogdisc  Store it anywhere, such as  session,cookie, Database ,php Documents  
-----------------------------------------------------------------------
*/

// We unregister this class here 
unset($ourfirstdog);

/*   Restore operation   */

/* 
-----------------------------------------------------------------------
   Where you put the string  $dogdisc  Read it from where you store it, such as  session,cookie, Database ,php Documents  
-----------------------------------------------------------------------
*/


// We use it here  unserialize()  Restore a serialized object 
$pet = unserialize($dogdisc); // At this time  $pet  It's already the previous one  $ourfirstdog  Object 
// Get the age and name attributes 
$old = $pet->getage();
$name = $pet->getname();
// This class can continue to be used without instantiation at this time , And the properties and values are kept in the state before serialization 
print "Our first dog is called $name and is $old days old<br>";
print '<BR>';
?>

We can also change the object in the example to other types such as arrays, and the effect is 1!

In fact, serialize () is the PHP variables such as objects (object), arrays (array) values serialized into strings and stored. Serialized strings can be stored in other places such as databases, Session, Cookie, etc. Serialization does not lose the type and structure of these values. The data for these variables can then be passed between PHP pages, or even between different PHP programs.

unserialize () converts the serialized string back to the value of PHP.

Here again quote 1 paragraph PHP manual instructions, look at the above example, 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 except resource. Even serialize () contains arrays pointing to their own references. The reference in the array/object of serialize () will also be stored.

When an object is serialized, PHP attempts to call the object's member function __sleep () before the sequence action. This allows the object to do any cleanup before being serialized. Similarly, when an object is restored using unserialize (), the __wakeup () member function is called

unserialize () operates on the serialized variable of single 1 to convert it back to the value of PHP. Returns the converted value, which can be integer, float, string, array, or object. If the passed string is not de-serialized, FALSE is returned.


Related articles: