Serialization of Magic Functions __sleep () and deserialization magic function __wakeup ()

Source: Internet
Author: User
Tags string back

1.string Serialize (mixed $value)-produces a representation of a value that can be stored
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 value, use Unserialize ()

@ Serialized Array

  Serialize ($arr);

When setting a cookie, you can pass multiple values as a serialized array, and then deserialize the read on the receiving page

@ Serialization Object

Class Ren{private $name;p rivate $age, function __construct ($name, $age) {$this->name = $name; $this->age = $age;} Function Show () {echo ' name is: {$this->name}  Age is: {$this->age} ";} function __sleep () {   ///Magic Functions  Specify serialization of those contents, returned as an array, not defined all serialized return Array (' name ', ' age ');//return Array_ Keys (Get_object_vars ($this));}} $zao = new Ren ("Zhao Liu"); Echo serialize ($zao);

   __sleep () functions that are automatically executed when a serialization function is called to qualify the property to serialize

2.mixed unserialize (String $str [, String $callback])-Creates a value for PHP from a stored representation
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

Note:

After the object is serialized, only the properties of the stored object are serialized, and the method is not serialized (because the method belongs to a class and does not belong to a single object)
When deserializing, simply deserialize the property, using the __wakeup () Magic function, after successfully re-constructing the object, PHP will automatically attempt to invoke the __wakeup () member function (if one exists).

Serialization of objects

Class DB {private $host;p rivate $user;p rivate $pwd;p rivate $dbname;p rivate $mysqli; function __construct ($host, $user, $ PWD, $dbname) {$this->host = $host; $this->user = $user; $this->pwd = $pwd; $this->dbname = $dbname; $this db ();} function db () {$this->mysqli = new Mysqli ($this->host, $this->user, $this->pwd, $this->dbname);} function Select () {$this->mysqli->query ("SET CHARSET GBK"), $sql = "Select Id,cname from Hdw_channel"; $result = $thi S->mysqli->query ($sql), $rows = Array (), while ($row = $result->fetch_assoc ()) {$rows [] = $row;} ECHO "<PRE>";p Rint_r ($rows);} function __wakeup () {  //deserialization automatically runs the linked database $this->db ();}} Session_Start (); $chanel = new db ("localhost", ' root ', ' ', ' hdcms ');//$chanel->select (); $_session[' channel_obj '] = Serialize ($chanel);

Receive page, deserialize

Session_Start (); include ' 59.php '; Load such files $channel_obj=unserialize ($_session[' channel_obj '); Deserialization object $channel_obj->select ();

 __wakeup () Magic function is called automatically when deserializing (if defined within the class)

Serialization of Magic Functions __sleep () and deserialization magic function __wakeup ()

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.