PHP serialize () serialized function _ PHP Tutorial

Source: Internet
Author: User
Serialize () serialization function of PHP. The explanations of serialize () and unserialize () in the php Manual are: serialize-Generatesastorablerepresentationofavalue, which generates a representation of the stored values. Unserialize-crecreserialize () 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:

 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'
';/* Token here you can store the string $ dogdisc anywhere, such as session, cookie, database, and PHP file metadata * // Here we deregister this class unset ($ ourfirstdog ); /* restore operation * // Here, the string $ dogdisc is read from the place where you store it, such as session, cookie, database, php file restart * // Here we use unserialize () to restore the serialized object $ 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, and both attributes and values are in the state before serialization print "Our first dog is called $ name and is $ old days old
"; Print'
';?>

An official program example:

 

The http://www.bkjia.com/PHPjc/752373.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752373.htmlTechArticleserialize () and unserialize () explained in the php Manual are: serialize-Generates a storable representation of a value, which produces a representation of a stored value. Unserialize-Cre...

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.