PHP serialize () serialization function _php Tutorial

Source: Internet
Author: User
Tags string back
Serialize () and unserialize () are explained in the PHP manual as:

Serialize-generates a storable representation of a value, producing a representation of the value that can be stored.

Unserialize-creates a PHP value from a stored representation to create a PHP value from the stored representation.

Serialize () returns a string that contains a byte stream representing value that can be stored anywhere. This facilitates the storage or delivery of PHP values without losing their type and structure.

To change the serialized string back to PHP's value, use Unserialize (). Serialize () can handle any type except resource. You can even serialize () those arrays that contain pointers to their own references. The references in the array/object you are serialize () will also be stored.

When serializing an object, PHP will attempt to call the object's member function __sleep () before the sequence action. This allows the object to do any cleanup before it is serialized. Similarly, when you use Unserialize () to restore an object, the __wakeup () member function is called.

Let's try the method of using 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); }}//instantiation of this class $ourfirstdog = new Dog ("Rover", "Lisa and Graham");//Use the Serialize function to convert this instance into a serialized string $dogdisc = Serialize ($ Ourfirstdog);p rint $dogdisc; The $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 put the string $ Dogdisc stored in any place such as Session,cookie, database, PHP files----------------------------------------------------------------------- ------------------*///We write off this class unset ($ourfirstdog);/* Restore Operation *//*------------------------------------------------- ----------------------------------------here to read the string $dogdisc from where you stored it, such as Session,cookie, database, PHP file-------------------- ---------------------------------------------------------------------*///We're here with unserialize () to restore the serialized object $pet = Unserialize ($DOGDISC); At this point the $pet is already the front $ourfirstdog object//Get the Age and name attribute $old = $pet->getage (); $name = $pet->getname ();//This class does not need to be instantiated at this time to continue to use, And the properties and values are kept in the state before serialization print "Our first dog is called $name and is $old days old
";p rint '
';? >

An example of an official procedure:

 
  

http://www.bkjia.com/PHPjc/752373.html www.bkjia.com true http://www.bkjia.com/PHPjc/752373.html techarticle serialize () and unserialize () are interpreted in the PHP manual as: serialize-generates a storable representation of a value, producing a representation of the value that can be stored. 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.