Learn the usage of PHP serialize () and Unserialize ()

Source: Internet
Author: User
Tags string back

Serialize () and unserialize () are explained in the PHP Manual: Serialize-generates A storable representation of a valueserialize-produces a representation of a value that can be stored

Unserialize-creates a PHP value from a stored representationunserialize-to create PHP values from a stored representation

Obviously, the interpretation of "a stored representation" has been translated into a value that can be stored and remains very confusing.

Use an example to see how these two functions are used:

     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 store the string $dogdisc anywhere, such as a session, Cookies, databases, PHP files-----------------------------------------------------------------------*///We hereby unregister this class unset ($ Ourfirstdog);/* Restore operation *//*-----------------------------------------------------------------------here to $dogdisc the string Read it from where you stored it, such as Session,cookie, database, PHP files-----------------------------------------------------------------------*/// Here we use Unserialize () to restore the already 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 '
';? >

Examples of objects we can also change the array and other types, the effect is the same!

In fact serialize () is to store the values of variables such as objects (object), arrays, and so on in PHP as strings. Serialized strings We can store them in other places such as databases, sessions, cookies, etc. The serialized operation does not lose the type and structure of these values. So the data for these variables can be passed between PHP pages and even different PHP programs.

The Unserialize () is the value of converting the serialized string back to PHP.

Here is a reference to a PHP manual, read the above example, it should be easy to understand the meaning of the following words

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

Unserialize () operates on a single serialized variable and converts it back to the value of PHP. Returns the converted value, which can be an integer, float, string, array, or object. Returns FALSE if the passed string is not serializable.

  • 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.