PHP function serialize () and unserialize () ____ function

Source: Internet
Author: User

Serialize () and unserialize() are explained in the PHP manual:

Serialize -generates a storable representation of a value

Serialize -represents a value that can be stored

unserialize -creates A PHP value from a stored representation

unserialize-Create a PHP value from a stored representation

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

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.