Php object serialization and deserialization PHP object storage and transmission

Source: Internet
Author: User
Php object serialization and deserialization PHP object storage and transmission

  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. $ P1 = new Person ("zhang san", 20 );

  13. $ P1_string = serialize ($ p1); // serialize the object and write it to the file.
  14. $ Fh = fopen ("p1.text", "w ");
  15. Fwrite ($ fh, $ p1_string );
  16. Fclose ($ fh );
  17. ?>

Open the p1.text file and enter the following content: O: 6: "Person": 2: {s: 12: "Person name"; s: 4: "Zhang San"; s: 11: "Person age"; I: 20;}, but the characters generated by the serialization are not directly parsed.

II. 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. $ P2 = unserialize (file_get_contents ("p1.text "));

  13. $ P2-> say ();
  14. ?>

Output result: my name is Zhang San. my age is: 20. it indicates that the serialization object cannot serialize its method. Therefore, when unserialize is enabled, the current file must contain the corresponding class or the class file corresponding to require.

Serialization can only be used for a limited number of users, because each user needs to store or write files separately, and ensure that the file names cannot be repeated. When the user cannot exit the browser normally, the file cannot be deleted. The object is registered as a session variable. When there are many users, you can use session to save objects.

For more information about the session, see the article: Simple example of the session in php: php session operation class (attached with an instance) php session function set php session expiration settings detailed explanation of php session application example php Session technology session usage example php logout Session information Cookie and session usage in php5

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

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

Output result: my name is Zhang San. my age is: 20. like serialization, the method cannot be saved when the registered object is a session variable. Therefore, when reading session variables, the current file must contain the corresponding class or the class file corresponding to require.

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.