A brief discussion on the usage of php serialize() and unserialize()_PHP tutorial

WBOY
Release: 2016-07-21 15:09:17
Original
698 people have browsed it

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 explanation of "a stored representation" is still very confusing even after it is translated into a storable value.
If the language cannot be expressed clearly, then we can use a specific PHP example to learn the use of these two functions

Copy the code The code is as follows :

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->//Declare 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() {
                                                                                                                                                                                                                                                                                  " >
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 into 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 '
';
/*
-------------- -------------------------------------------------- -------
Here you can store the string $dogdisc anywhere such as session, cookie, database, php file
--------------- -------------------------------------------------- ------
*/
//We unregister this class here
unset($ourfirstdog);
/* Restore operation */
/*
-- -------------------------------------------------- ------------------
Here, read the string $dogdisc from the place where you store it, such as session, cookie, database, php file
-- -------------------------------------------------- ------------------
*/
//We use unserialize() here to restore the serialized object
$pet = unserialize( $dogdisc); //The $pet at this time is already the previous $ourfirstdog object
//Get the age and name attributes
$old = $pet->getage();
$name = $pet->getname();
//This class can continue to be used without instantiation, and the attributes and values ​​remain in the state before serialization
print "Our first dog is called $name and is $old days old
";
print '
';
?>


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, Session, Cookie, 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() converts the serialized string back into 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
If you want to change the serialized string back to a PHP value, You can 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
unserialize() will be called to operate on a single serialized variable and convert 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327345.htmlTechArticleThe explanation of serialize() and unserialize() in the PHP manual is: serialize — Generates a storable representation of a value serialize — Produce a storable representation of a value unserialize —...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!