The serialization of PHP

Source: Internet
Author: User
Serialization is the conversion of variable data to a string (unlike a type conversion mechanism), which is typically used to store data (files) and then recover in other cases (deserialization)
Serialization:

$val = serialize($var);file_put_contents('./*.txt',$val);

Deserialization:

$val = file_get_contents('./*.txt');$var = unserialize($val);

When an object is serialized, __sleep () in the class is called automatically;
When deserializing an object, the corresponding class's __wakeup () is automatically called, and the corresponding class declaration must be in the inverse sequence operation.

__sleep () can perform cleanup of some data (resources), and returns an array that stores the properties of objects that need to be serialized, that is, the properties can be picked for serialization.

classbee{public$a;    protected$b;    private$c;    publicfunction__sleep(){returnarray('a','b');      }}

__wakeup () perform some useful ' data recovery work ', such as connecting to a database, when doing a deserialization session

publicfunction__wakeup(){$this->func();}

__tostring ()
This method is called automatically when an object is treated as a string. This method allows you to return the appropriate string, which would be an error if the method is not defined.

classbee{publicfunction__tostring(){echo'string';    }}$objnew bee();//对象不能直接当字符串使用echo$obj;

__invoke ()
This method is called automatically when the object is used as a method

classbee{publicfunction__invoke(){echo'invoke';    }}$objnew bee();//对象不能直接当字符串使用echo$obj();

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the PHP serialization, including aspects of the content, I hope the PHP tutorial interested in a friend to help.

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