Home >Backend Development >PHP Tutorial >PHP uses serialize() and unserialize() to make objects into super variables_PHP tutorial
Original words and some summaries in the manual:
Description and cases of PHP functions serialize() and unserialize(). To convert 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.
serialize() returns a string, which contains a byte stream representing value and can be stored anywhere. This facilitates storing or passing PHP values without losing their type and structure.
To convert 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 an object is restored using unserialize(), the __wakeup() member function will be called.
Note: In PHP 3, object properties will be serialized, but methods will be lost. PHP 4 breaks this limitation and can store both properties and methods. See the Serialized Objects section in Classes and Objects for more information.
The 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 — Creates a PHP value from a stored representation
unserialize — Create a PHP value from a stored representation
serialize, translated as "serialize, make continuous", usually called "serialization"
This function is very useful, especially when used together with unserialize
I think the more useful place is when storing data in a database or recording it in a file
Of course, this kind of data must be relatively complex (not complicated and does not need to be serialized, I think it must be at least an array), and it must be a non-"index or primary key" in the database. Of course, it is best for this database field to be in the system and It has nothing to do with any search program. Of course, the data after serialization can still be searched, because the specific data has not been encrypted or changed
Example: (Just talk about objects, the rest should be easy)
CPerson.php
[php]
class Person
{
Public $age;
Function __construct($age)
{
$this->age=$age;
}
function walk()
{
echo "I'm walking...!";
}
}
?>
test0.php save the object here
[html]
session_start();
include_once './CPerson.php';
$person=new Person(22);
$b=serialize($person);
$_SESSION["object"]=$b;
?>
test1.php access object
[php]
session_start();
include_once './CPerson.php';
$p=$_SESSION['object'];
$a=unserialize($p);
$a->walk();
echo $a->age;
?>