Incomplete research on phpserialize () and unserialize ()

Source: Internet
Author: User
Tags php example string back
The description of serialize () and unserialize () in the php manual is: serializeGeneratesastorablerepresentationofavalueserialize generates a stored value indicating that the pipeline creates PHP from the stored Representation

Serialize () and unserialize () are described in the php manual as follows: serialize Generates a storable representation of a value serialize Generates a stored value that indicates unserialize Creates a PHP value from a stored representation unserialize Creates a PHP

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

Obviously ,"A stored representation"Is translatedA stored valueIt is still very confusing.

If the language cannot be clearly stated, we can use a specific PHP example to learn the usage of these two functions.

Code

// 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'
';

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

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

/* Restore operation */

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


// 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
";
Print'
';
?>

The objects in this example can also be converted into arrays and other types, with the same effect!

  In fact, serialize () is to serialize the values of PHP variables such as objects and arraysStringSerialized strings can be stored elsewhere, such as databases, sessions, and cookies. serialized operations do not lose the type and structure of these values. In this way, the data of these variables can be transmitted between PHP pages or even different PHP programs.

Unserialize () is to convert the serialized string back to the PHP value.

Here I will reference the instructions in the PHP manual. After reading the above example, it is easy to understand the meaning of the following statements.

To change serialized strings back to PHP values, use unserialize ().Serialize ()It can process any types except resource. EvenSerialize ()Contains arrays that point to their own references. You areSerialize ()The references in the array/object will also be stored.

When an object is serialized, PHP tries to call the member function _ sleep () of the object before the sequential action (). This allows objects to be cleared before being serialized. Similarly, when unserialize () is used to restore an object_ Wakeup ()Member Functions

Unserialize ()Operate a single serialized variable and convert it back to the PHP value. The returned value is the converted value, which can be integer, float, string, array, or object. If the passed string cannot be deserialized, returnFALSE.

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.