Serialize of PHP variable handling

Source: Internet
Author: User
Tags string back

Official definition:

String serialize (mixed $value)

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.

Serialization of objects

All values in PHP can be represented by using the function serialize () to return a string containing a stream of bytes. The Unserialize () function can re-change the string back to the original PHP value. Serializing an object saves all variables of the object, but does not save the object's methods, only the name of the class is saved.

To be able to unserialize () an object, the class of the object must already be defined. If an object of Class A is serialized, it returns a string that is related to Class A and contains all the variable values of the object. If you want to deserialize an object in another file, the class of the object must be defined before the solution is serialized, either by including a file that defines the class or by using the function Spl_autoload_register ().

The Official handbook shows that serialize () can serialize any type other than resource into a string to be stored in a variable or session.

code example:

Serialized array:

$arr = [' name ' = ' Tony ', ' age ' =>29, ' sex ' = ' male ']; $s = serialize ($arr); Var_dump ($s); Var_dump (Unserialize ($s ));

Output Result:

#1 the serialized string

String ("A:3:{s:4:" "Name"; s:4: "Tony"; s:3: "Age"; I:29;s:3: "Sex"; s:4: "Male";} "

#2 the original object after replying to the serialized string

Array (3) {
["Name"]=>
String (4) "Tony"
["Age"]=>
Int (29)
["Sex"]=>
String (4) "Male"
}

Serializing objects

The sample code in the manual is referenced here

<?php//classa.inc:    class A {public      $one = 1;          Public Function Show_one () {          echo $this->one;      }  }  page1.php:  include ("Classa.inc");    $a = new A;  $s = serialize ($a);  Save the variable $s so that the file page2.php can read  file_put_contents (' store ', $s);//page2.php:    //To properly understand serialization, you must include the following file  Include ("Classa.inc");  $s = file_get_contents (' store ');  $a = unserialize ($s);  You can now use the function Show_one () inside the object $ A  

Serializing an object in an application for later use strongly recommends that the entire application contain the definition of the object's class. Otherwise, there is a possibility that when the object is being deserialized, no definition of the object's class is found, and the class __php_incomplete_class_name without the method is used as the object's class, resulting in an unused object being returned.

So in the example above, when you run Session_register ("a") and put the variable $ A in the session, you need to include the file classa.inc on each page instead of just the file page1.php and page2.php.

Serialize of PHP variable handling

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.