Tutorials for PHP serialization of Serialize objects

Source: Internet
Author: User

Storage and transport of objects

In the actual project application, some tasks can not be completed in one or two pages, because the variables to the script after the completion of the release, the objects generated on this page would like to be used on other pages to encounter trouble.

If you need to pass objects and their methods to the page where we want to use the object, it is simpler and more feasible to either store the object or transfer it directly to the desired page, or register the object as a session variable.

Serializing objects

Object serialization is the conversion of an object into a stream of bytes that can be stored. The object needs to be serialized when we need to transfer an object to the network or to write the object to a file or database.

The serialization complete process consists of two steps: One is serialization, which is the conversion of an object into a binary string, the serialize () function is used to serialize an object, and the other is deserialization, which converts the binary string of the object into an object, Unserialize () function to deserialize an object that is serialized. As a whole, the type structure and data within the object are complete.

Grammar:

String serialize (mixed value) mixed unserialize (String str [, String callback])

Example:

name = $name;        $this->age = $age;    }    function say () {echo "My name is:". $this->name. "; echo "My Age is:". $this->age;    }} $p 1 = new Person ("Zhang San"), $p 1_string = serialize ($p 1);//write the object after serializing the file $fh = fopen ("P1.text", "w"); Fwrite ($fh, $p 1_string); Fclose ($FH);? >

Open the P1.text file and write the following:

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.

Deserialization:

name = $name;        $this->age = $age;    }    function say () {echo "My name is:". $this->name. "; echo "My Age is:". $this->age;    }} $p 2 = unserialize (file_get_contents ("P1.text")), $p 2-Say ();? >

To run the example, output:

My name is: Zhang San my age is: 20

Tips

    1. Because the serialized object cannot serialize its methods, the current file must contain the corresponding class or require the corresponding class file at unserialize time.

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

      object is registered as 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 "PHP session".

      Example:

      name = $name;    $this->age = $age; } function say () {echo "My name is:". $this->name. ";    echo "My Age is:". $this->age; }}$_session["P1"] = new Person ("Zhang San", 20);?        

      Read session:

      name = $name;    $this->age = $age; } function say () {echo "My name is:". $this->name. ";    echo "My Age is:". $this->age; }}$_session["P1"], say ();? 

      Run the example, output:

       My name is: Zhang San my age is: 

      As with serialization, the registration object is session Variable does not save its method, so 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.