PHP functions: serialize () and unserialize ()

Source: Internet
Author: User

Serialize() AndUnserialize() The explanation in the PHP manual is:

Serialize-Generates a storable representation of a value

Serialize-Generate a representation of a stored value

Unserialize-Creates a PHP value from a stored Representation

Unserialize-Create a PHP value from a stored Representation

<? PHP
// 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 (){
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 the serialize function to convert this instance into a serialized string
$ 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>';

/*
Bytes -----------------------------------------------------------------------------------------
Here you can store the string $ dogdisc anywhere, such as Session, Cookie, database, and PHP files.
Bytes -----------------------------------------------------------------------------------------
*/

// Cancel this class here
Unset($ Ourfirstdog);

/* Restore operation */

/*
Bytes -----------------------------------------------------------------------------------------
Here, you can read the string $ dogdisc from your storage location, such as Session, Cookie, database, and PHP files.
Bytes -----------------------------------------------------------------------------------------
*/

// Here we use unserialize () to restore serialized objects.
$ Pet = Unserialize ( $ Dogdisc );// $ PET is the previous $ ourfirstdog object.
// Get the age and name attributes
$ Old = $ Pet -> Getage ();
$ Name = $ Pet -> Getname ();
// This class can be used without instantiation at this time, and both attributes and values remain in the state before serialization.
Print "Our First Dog is called $ Name And is $ 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.