Home  >  Article  >  Backend Development  >  Learn how to use php serialize() and unserialize()

Learn how to use php serialize() and unserialize()

WBOY
WBOYOriginal
2016-07-25 09:11:451211browse

serialize() and unserialize() are explained in the PHP manual: serialize — Generates a storable representation of a value serialize — Produce 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 explanation of "a stored representation" is still very confusing after being translated into a storable value.

Let’s take a look at the usage of these two functions through an example:

<?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 '<BR>';

/* 
-----------------------------------------------------------------------
    在这里你可以将字符串 $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 '<BR>';
?>

  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, arrays, etc. into strings and store them. We can store the serialized strings in other places such as databases, Sessions, etc. Cookies, etc., the serialization operation does 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

 To change a serialized string back into a PHP value, use unserialize(). 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.



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