PHP serialization Operation method Analysis _php Skill

Source: Internet
Author: User
Tags php programming serialization

This article illustrates the method of PHP serialization operation. Share to everyone for your reference, specific as follows:

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 revert (deserialize) in other cases

Serialization:

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

Deserialization:

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

When an object is serialized, the __sleep () in the class is automatically invoked.

When deserializing an object, the __wakeup () of the corresponding class is automatically invoked, and the corresponding class declaration must be available in the reverse sequence operation.

In __sleep () , you can perform a cleanup shutdown of some data (resources) and return an array that stores the properties of the objects that need to be serialized, that is, you can select attributes for serialization.

Class bee{public
 $a;
 protected $b;
 Private $c;
 Public Function __sleep () {return
  array (' A ', ' B ');
  }


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

Public Function __wakeup () {
 $this->func ();
}

__tostring ()

This method is automatically invoked when an object is treated as a string. This method allows you to return the appropriate string, and an error occurs if the method is not defined.

Class bee{public
 function __tostring () {
  echo ' string ';
 }
}
$obj = new Bee ();
Object cannot be used directly when the string uses the
echo $obj;

__invoke ()

This method is automatically invoked when an object is used as a method

Class bee{public
 function __invoke () {
  echo ' invoke ';
 }
}
$obj = new Bee ();
Object cannot be used directly as a string using the
echo $obj ();

For more information about PHP interested readers can view the site topics: "PHP string (String) Usage summary", "PHP array Operation techniques Encyclopedia", "PHP basic Grammar Introductory Course", "PHP Operations and Operator Usage Summary", "PHP Network programming Skills Summary" , "Introduction to PHP object-oriented Programming", "PHP+MYSQL database operations to get Started" and "PHP common database Operation Skills Summary"

I hope this article will help you with your PHP programming.

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.