Serialize () serialization function of PHP

Source: Internet
Author: User

Serialize () and unserialize () are described in the php manual as follows:

Serialize-Generates a storable representation of a value to generate a representation of a stored value.

Unserialize-Creates a PHP value from a stored representation, which Creates the PHP value from the stored representation.

Serialize () returns a string that contains a byte stream that represents value and can be stored anywhere. This facilitates storing or passing PHP values without losing their types and structures.

To change serialized strings back to PHP values, use unserialize (). Serialize () can process any type other than resource. Even serialize () contains arrays that point to its own reference. References in arrays/objects of serialize () 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, the _ wakeup () member function is called.

Next, let's try the usage of this function:

<? Php // declare a 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/>';/* Here you can store the string $ dogdisc anywhere, such as session, cookie, database, PHP file --------------------------------------------- ---------------------------------------- * // Here We deregister this class unset ($ ourfirstdog);/* restore operation * // the handler here reads the string $ dogdisc from the place where you store it for session, cookie, database, and PHP file exist * // here we use unserialize () to restore the serialized object $ pet = unserialize ($ dogdisc); // At this time $ pet has The previous $ ourfirstdog object is passed. // get the age and name attributes $ old = $ pet-> getage (); $ name = $ pet-> getname (); // This class can be used without instantiation at this time, in addition, both attributes and values remain in the status before serialization print "Our first dog is called $ name and is $ old days old <br/> "; print '<br/>';?>

An official program example:

<? Php // $ session_data is a multi-dimensional array containing the current user session information. // We use serialize () to store it in the database before the request ends. $ Conn = odbc_connect ("webdb", "php", "chicken"); $ stmt = odbc_prepare ($ conn, "UPDATE sessions SET data =? WHERE id =? "); $ Sqldata = array (serialize ($ session_data), $ PHP_AUTH_USER); if (! Odbc_execute ($ stmt, & $ sqldata) {$ stmt = odbc_prepare ($ conn, "insert into sessions (id, data) VALUES (?, ?) "); If (! Odbc_execute ($ stmt, & $ sqldata) {/* Error */}?>

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.