PHP function serialize () and unserialize () usage examples _php techniques

Source: Internet
Author: User
Tags serialization string back

This article illustrates the use of PHP functions serialize () and Unserialize (). Share to everyone for your reference. The specific methods are as follows:

This example mainly describes the PHP function serialize () and unserialize () description and case. To change the serialized string back to the value of PHP, use Unserialize (). Serialize () can handle any type other than resource. You can even serialize () those that contain arrays that point to their own references. The reference in the array/object you are serialize () will also be stored.

Serialize () returns a string containing the byte stream representing value, which can be stored anywhere. This facilitates storing or passing PHP values without losing their type and structure.

To change the serialized string back to the value of PHP, use Unserialize (). Serialize () can handle any type other than resource. You can even serialize () those that contain arrays that point to their own references. The reference in the array/object you are serialize () will also be stored.

When serializing an object, PHP attempts to invoke the member function __sleep () of the object before the sequence action. This allows the object to do any cleanup operations before being serialized. Similarly, the __wakeup () member function is invoked when the object is recovered using Unserialize ().

Note: In PHP 3, the object properties will be serialized, but the method will be lost. PHP 4 Breaks This limit and can store both properties and methods. See the serialization object section of the class and object for more information.
Serialize () and unserialize () are explained in the PHP manual:

Serialize-generates a storable representation of a value
Serialize-produces a representation of a value that can be stored
Unserialize-creates a PHP value from a stored representation
unserialize-create PHP values from stored representations
Serialize, translated is called "Serial, make continuous", usually called it "serialization"

This function is very useful, especially with unserialize
I think the more useful place is to put the data into the database or record it in the file.
Of course, this data must be more complex (no complexity and no need to serialize, I think at least an array), but also the database is not "index or primary key", of course, this database field in the system and any search program is irrelevant, Of course, the data after the serialize is still able to search, because the specific data has not been encrypted or changed

Copy Code code as follows:
<?php
A little simpler.
$array = Array ();
$array [' key '] = ' website ';
$array [' Value ']= ' www.isoji.org ';
$a = serialize ($array);
echo $a;
Unset ($array);
$a = unserialize ($a);
Print_r ($a);

Declaring 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 () {
Return ($this->age * 365);
}

function GetOwner () {
Return ($this->owner);
}

function GetName () {
Return ($this->name);
}
}
Instantiation of this class
$ourfirstdog = new Dog ("Rover", "Lisa and Graham");
Converts this instance to a serialized string using the Serialize function
$dogdisc = serialize ($ourfirstdog);
Print $dogdisc; $ourfirstdog has been serialized as 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 store the string $dogdisc anywhere such as Session,cookie, database, PHP file
*/
We're canceling this class here.
Unset ($ourfirstdog);
/* Restore Operation * *
/*
Here to read the string $dogdisc from where you store it like Session,cookie, database, PHP file
*/

We're here to restore an object that has been serialized with Unserialize ()
$pet = Unserialize ($dogdisc); The $pet at this point is already $ourfirstdog object in front of you.
Get age and Name attributes
$old = $pet->getage ();
$name = $pet->getname ();
This class does not need to be instantiated at this time to continue to use, and both the property and the value remain in the state before the serialization
Print "Our A-dog is called $name and are $old days old<br>";
print ' <BR> ';
?>

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.