Serialization of PHP objects and deserialization of PHP object storage and Transfer

Source: Internet
Author: User
Tags php session
    1. Class Person {

    2. Private $name;
    3. Private $age;
    4. function __construct ($name, $age) {
    5. $this->name = $name;
    6. $this->age = $age;
    7. }

    8. function say () {

    9. echo "My name is:" $this->name. "
      ";
    10. echo "My Age is:". $this->age;
    11. }}

    12. $p 1 = new Person ("Zhang San", 20);

    13. $p 1_string = serialize ($p 1);//write to file after serializing object
    14. $fh = fopen ("P1.text", "w");
    15. Fwrite ($fh, $p 1_string);
    16. Fclose ($FH);
    17. ?>

Copy Code

Open P1.text file, enter: o:6: ' Person ': 2:{s:12: ' person name '; s:4: ' Zhang San '; s:11: ' Person of age '; i:20;} However, it is not usually straightforward to parse the characters generated by the above serialization.

Two, deserialization:

    1. Class Person {

    2. Private $name;
    3. Private $age;
    4. function __construct ($name, $age) {
    5. $this->name = $name;
    6. $this->age = $age;
    7. }
    8. function say () {
    9. echo "My name is:" $this->name. "
      ";
    10. echo "My Age is:". $this->age;
    11. }}

    12. $p 2 = unserialize (file_get_contents ("P1.text"));

    13. $p 2, say ();
    14. ?>
Copy Code

Output: My name is: Zhang San my age is: 20 tip because the serialized object cannot serialize its methods, the current file must contain the corresponding class or require class file when unserialize.

Serialization is only available for limited users, because files need to be stored or written separately for each user, and the file name cannot be duplicated. In the event that the user does not exit the browser normally, the file is not guaranteed to be deleted. The object is registered as a session variable. When you have a large number of users, consider using the session to save the object.

For more information about the session, see the article: A simple example of a session in PHP PHP session operation Class (with instance) PHP Session function set method of session expiration in PHP how to use the session in PHP application example PHP sessions usage example PHP logoff session information PHP5 cookie and session usage

Example:

    1. Session_Start ();

    2. Class Person {
    3. Private $name;
    4. Private $age;
    5. function __construct ($name, $age) {
    6. $this->name = $name;
    7. $this->age = $age; }
    8. function say () {
    9. echo "My name is:" $this->name. "
      ";
    10. echo "My Age is:". $this->age;
    11. }}

    12. $_session["P1"] = new Person ("Zhang San", 20);

    13. ?>

Copy Code

Read session:

    1. Session_Start ();

    2. Class Person {
    3. Private $name;
    4. Private $age;
    5. function __construct ($name, $age) {
    6. $this->name = $name;
    7. $this->age = $age;
    8. }

    9. function say () {

    10. echo "My name is:" $this->name. "
      ";
    11. echo "My Age is:". $this->age;
    12. }}
    13. $_session["P1"], say ();
    14. ?>
Copy Code

Output: My name is: Zhang San my age is: 20 as with serialization, registering an object as a session variable does not save its method. Therefore, when reading the session variable, the current file must contain the corresponding class or require corresponding class file.

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