php Function serialize of and unserialize of Usage Examples

  • 2021-07-24 10:35:58
  • OfStack

This article illustrates the usage of php functions serialize () and unserialize (). Share it for your reference. The specific methods are as follows:

This example mainly describes the php function serialize () and unserialize () explanation and case. 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 its own references. The reference in the array/object of serialize () will also be stored.

serialize () returns a string that contains a stream of bytes representing value and can be stored anywhere. This is beneficial to store or pass the value of PHP without losing its type and structure.

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 its 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.

Note: In PHP 3, object properties are serialized, but methods are lost. PHP 4 breaks this limitation by storing both properties and methods. See the Serializing Objects section in Classes and Objects for more information.
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-Creates the value of PHP from the stored representation
serialize, translated as "serialization, making continuous", is usually called "serialization"

This function is very useful, especially when used with unserialize1
I think it is useful to store data in a database or record it in a file
Of course, this kind of data must be more complex (it is not complicated and does not need serialize, I think it must be at least one 1 array). Moreover, it is not an "index or primary key" in the database. Of course, it is best that this database field has nothing to do with any search program in the system. Of course, the data after serialize can still be searched because the specific data has not been encrypted or changed

<?php
// Simple 1 Point
$array = array();
$array['key'] = 'website';
$array['value']='www.isoji.org';
$a = serialize($array);
echo $a;
unset($array);
$a = unserialize($a);
print_r($a); // 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>';
?>

I hope this article is helpful to everyone's PHP programming.


Related articles: