Home >Backend Development >PHP Tutorial >The difference between php serialize() and unserialize()

The difference between php serialize() and unserialize()

墨辰丷
墨辰丷Original
2018-05-17 14:01:412424browse

This article mainly introduces some differences between php serialize() and unserialize(). Friends in need can refer to

The explanation of serialize() and unserialize() in the php manual is:

serialize — Generates a storable representation of a value

serialize — Generates a storable representation of a value

unserialize — Creates a PHP value from a stored representation

unserialize — Create a PHP value from a stored representation

Obviously, the interpretation of "a stored representation" is translated into a storable value It's still very confusing to understand what it means.

If the language cannot be expressed clearly, then we can use a specific PHP example to learn the use of these two functions

<?php
//声明一个类
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);
  }
}
//实例化这个类
$ourfirstdog = new dog("Rover",12,"Lisa and Graham");
//用serialize函数将这个实例转化为一个序列化的字符串
$dogdisc = serialize($ourfirstdog);
print $dogdisc; //$ourfirstdog 已经序列化为字符串 O:3:"dog":3:{s:4:"name";s:5:"Rover";s:3:"age";i:12;s:5:"owner";s:15:"Lisa and Graham";}

print &#39;<BR>&#39;;

/* 
-----------------------------------------------------------------------
  在这里你可以将字符串 $dogdisc 存储到任何地方如 session,cookie,数据库,php文件 
-----------------------------------------------------------------------
*/

//我们在此注销这个类
unset($ourfirstdog);

/*  还原操作  */

/* 
-----------------------------------------------------------------------
  在这里将字符串 $dogdisc 从你存储的地方读出来如 session,cookie,数据库,php文件 
-----------------------------------------------------------------------
*/


//我们在这里用 unserialize() 还原已经序列化的对象
$pet = unserialize($dogdisc); //此时的 $pet 已经是前面的 $ourfirstdog 对象了
//获得年龄和名字属性
$old = $pet->getage();
$name = $pet->getname();
//这个类此时无需实例化可以继续使用,而且属性和值都是保持在序列化之前的状态
print "Our first dog is called $name and is $old days old<br>";
print &#39;<BR>&#39;;
?>

We can also change the objects in the example to other types such as arrays, and the effect will be the same!

In fact, serialize() is to serialize the values ​​​​of variables in PHP such as objects (objects), arrays (array), etc. into strings and store them. We can store the serialized strings in other places Such as database, Session, Cookie, etc., the serialization operation will not lose the type and structure of these values. In this way, the data of these variables can be passed between PHP pages and even different PHP programs.

And unserialize() is to convert the serialized string back to a PHP value.

Here is another quote from the PHP manual. After reading the above example, it should be easy to understand the meaning of the following words

Want to change the serialized string back to PHP For values, unserialize() can be used. serialize() can handle any type except resource. You can even serialize() arrays that contain references to themselves. References in the array/object you are serializing() will also be stored.

When serializing an object, PHP will attempt to call the object's member function __sleep() before the sequence action. This allows any cleanup operations to be done before the object is serialized. Similarly, when using unserialize() to restore an object, the __wakeup() member function

will be called. unserialize() operates on a single serialized variable and converts it back to a PHP value. What is returned is the converted value, which can be integer, float, string, array or object. Returns FALSE if the passed string is not deserializable.

Related recommendations:

Detailed explanation of serialize() and unserialize() function examples in php

Detailed explanation of form formatting plug-in jquery.serializeJSON

Ajax serialize() form to serialize the file upload

The above is the detailed content of The difference between php serialize() and unserialize(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn